summaryrefslogtreecommitdiffstats
path: root/firmware/export/jz4760b.h
blob: 589f67800aa424d0bf5f9dd08737764ce27cef25 (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
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2016 by Roman Stolyarov
 * Copyright (C) 2008 Ingenic Semiconductor 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

/*
 *  jz4760b.h
 *
 *  JZ4760B common definition.
 *
 *  Copyright (C) 2008 Ingenic Semiconductor Inc.
 *
 *  Author: <cwjia@ingenic.cn>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef __JZ4760B_H__
#define __JZ4760B_H__

#if defined(__ASSEMBLY__) || defined(__LANGUAGE_ASSEMBLY)
        #ifndef __MIPS_ASSEMBLER
                #define __MIPS_ASSEMBLER
        #endif
        #define REG8(addr)	(addr)
        #define REG16(addr)	(addr)
        #define REG32(addr)	(addr)
#else
	typedef unsigned char u8;
	typedef unsigned short u16;
	typedef unsigned int u32;

        #define REG8(addr)	*((volatile unsigned char *)(addr))
        #define REG16(addr)	*((volatile unsigned short *)(addr))
        #define REG32(addr)	*((volatile unsigned int *)(addr))

        #define INREG8(x)               ((unsigned char)(*(volatile unsigned char *)(x)))
        #define OUTREG8(x, y)           *(volatile unsigned char *)(x) = (y)
        #define SETREG8(x, y)           OUTREG8(x, INREG8(x)|(y))
        #define CLRREG8(x, y)           OUTREG8(x, INREG8(x)&~(y))
        #define CMSREG8(x, y, m)        OUTREG8(x, (INREG8(x)&~(m))|(y))

        #define INREG16(x)              ((unsigned short)(*(volatile unsigned short *)(x)))
        #define OUTREG16(x, y)          *(volatile unsigned short *)(x) = (y)
        #define SETREG16(x, y)          OUTREG16(x, INREG16(x)|(y))
        #define CLRREG16(x, y)          OUTREG16(x, INREG16(x)&~(y))
        #define CMSREG16(x, y, m)       OUTREG16(x, (INREG16(x)&~(m))|(y))

        #define INREG32(x)              ((unsigned int)(*(volatile unsigned int *)(x)))
        #define OUTREG32(x, y)          *(volatile unsigned int *)(x) = (y)
        #define SETREG32(x, y)          OUTREG32(x, INREG32(x)|(y))
        #define CLRREG32(x, y)          OUTREG32(x, INREG32(x)&~(y))
        #define CMSREG32(x, y, m)       OUTREG32(x, (INREG32(x)&~(m))|(y))
#endif

/*
 * Define the bit field macro to avoid the bit mistake
 */
#define BIT0            (1 << 0)
#define BIT1            (1 << 1)
#define BIT2            (1 << 2)
#define BIT3            (1 << 3)
#define BIT4            (1 << 4)
#define BIT5            (1 << 5)
#define BIT6            (1 << 6)
#define BIT7            (1 << 7)
#define BIT8            (1 << 8)
#define BIT9            (1 << 9)
#define BIT10           (1 << 10)
#define BIT11           (1 << 11)
#define BIT12 	        (1 << 12)
#define BIT13 	        (1 << 13)
#define BIT14 	        (1 << 14)
#define BIT15 	        (1 << 15)
#define BIT16 	        (1 << 16)
#define BIT17 	        (1 << 17)
#define BIT18 	        (1 << 18)
#define BIT19 	        (1 << 19)
#define BIT20 	        (1 << 20)
#define BIT21 	        (1 << 21)
#define BIT22 	        (1 << 22)
#define BIT23 	        (1 << 23)
#define BIT24 	        (1 << 24)
#define BIT25 	        (1 << 25)
#define BIT26 	        (1 << 26)
#define BIT27 	        (1 << 27)
#define BIT28 	        (1 << 28)
#define BIT29 	        (1 << 29)
#define BIT30 	        (1 << 30)
#define BIT31 	        (1 << 31)

/* Generate the bit field mask from msb to lsb */
#define BITS_H2L(msb, lsb)  ((0xFFFFFFFF >> (32-((msb)-(lsb)+1))) << (lsb))

/* Get the bit field value from the data which is read from the register */
#define get_bf_value(data, lsb, mask)  (((data) & (mask)) >> (lsb))

/*
 * Define the module base addresses
 */
/* AHB0 BUS Devices Base */
#define HARB0_BASE	0xB3000000
/* AHB1 BUS Devices Base */
#define HARB1_BASE	0xB3200000
#define	DMAGP0_BASE	0xB3210000
#define	DMAGP1_BASE	0xB3220000
#define	DMAGP2_BASE	0xB3230000
#define	DEBLK_BASE	0xB3270000
#define	IDCT_BASE	0xB3280000
#define	CABAC_BASE	0xB3290000
#define	TCSM0_BASE	0xB32B0000
#define	TCSM1_BASE	0xB32C0000
#define	SRAM_BASE	0xB32D0000
/* AHB2 BUS Devices Base */
#define HARB2_BASE	0xB3400000
#define UHC_BASE	0xB3430000
#define GPS_BASE	0xB3480000
#define ETHC_BASE	0xB34B0000
/* APB BUS Devices Base */
#define	PS2_BASE	0xB0060000

/*
 * General purpose I/O port module(GPIO) address definition
 */
#define	GPIO_BASE	0xb0010000

/* GPIO group offset */
#define GPIO_GOS	0x100

/* Each group address */
#define GPIO_BASEA	(GPIO_BASE + (0) * GPIO_GOS)
#define GPIO_BASEB	(GPIO_BASE + (1) * GPIO_GOS)
#define GPIO_BASEC	(GPIO_BASE + (2) * GPIO_GOS)
#define GPIO_BASED	(GPIO_BASE + (3) * GPIO_GOS)
#define GPIO_BASEE	(GPIO_BASE + (4) * GPIO_GOS)
#define GPIO_BASEF	(GPIO_BASE + (5) * GPIO_GOS)

/*
 * GPIO registers offset address definition
 */
#define GPIO_PXPIN_OFFSET	(0x00)	/*  r, 32, 0x00000000 */
#define GPIO_PXDAT_OFFSET	(0x10)	/*  r, 32, 0x00000000 */
#define GPIO_PXDATS_OFFSET	(0x14)  /*  w, 32, 0x???????? */
#define GPIO_PXDATC_OFFSET	(0x18)  /*  w, 32, 0x???????? */
#define GPIO_PXIM_OFFSET	(0x20)  /*  r, 32, 0xffffffff */
#define GPIO_PXIMS_OFFSET	(0x24)  /*  w, 32, 0x???????? */
#define GPIO_PXIMC_OFFSET	(0x28)  /*  w, 32, 0x???????? */
#define GPIO_PXPE_OFFSET	(0x30)  /*  r, 32, 0x00000000 */
#define GPIO_PXPES_OFFSET	(0x34)  /*  w, 32, 0x???????? */
#define GPIO_PXPEC_OFFSET	(0x38)  /*  w, 32, 0x???????? */
#define GPIO_PXFUN_OFFSET	(0x40)  /*  r, 32, 0x00000000 */
#define GPIO_PXFUNS_OFFSET	(0x44)  /*  w, 32, 0x???????? */
#define GPIO_PXFUNC_OFFSET	(0x48)  /*  w, 32, 0x???????? */
#define GPIO_PXSEL_OFFSET	(0x50)  /*  r, 32, 0x00000000 */
#define GPIO_PXSELS_OFFSET	(0x54)  /*  w, 32, 0x???????? */
#define GPIO_PXSELC_OFFSET	(0x58)  /*  w, 32, 0x???????? */
#define GPIO_PXDIR_OFFSET	(0x60)  /*  r, 32, 0x00000000 */
#define GPIO_PXDIRS_OFFSET	(0x64)  /*  w, 32, 0x???????? */
#define GPIO_PXDIRC_OFFSET	(0x68)  /*  w, 32, 0x???????? */
#define GPIO_PXTRG_OFFSET	(0x70)  /*  r, 32, 0x00000000 */
#define GPIO_PXTRGS_OFFSET	(0x74)  /*  w, 32, 0x???????? */
#define GPIO_PXTRGC_OFFSET	(0x78)  /*  w, 32, 0x???????? */
#define GPIO_PXFLG_OFFSET	(0x80)  /*  r, 32, 0x00000000 */
#define GPIO_PXFLGC_OFFSET	(GPIO_PXDATS_OFFSET)  /*  w, 32, 0x???????? */
#define GPIO_PXDS0_OFFSET	(0xC0)  /*  r, 32, 0x00000000 */
#define GPIO_PXDS0S_OFFSET	(0xC4)  /*  w, 32, 0x00000000 */
#define GPIO_PXDS0C_OFFSET	(0xC8)  /*  w, 32, 0x00000000 */
#define GPIO_PXDS1_OFFSET	(0xD0)  /*  r, 32, 0x00000000 */
#define GPIO_PXDS1S_OFFSET	(0xD4)  /*  w, 32, 0x00000000 */
#define GPIO_PXDS1C_OFFSET	(0xD8)  /*  w, 32, 0x00000000 */
#define GPIO_PXDS2_OFFSET	(0xE0)  /*  r, 32, 0x00000000 */
#define GPIO_PXDS2S_OFFSET	(0xE4)  /*  w, 32, 0x00000000 */
#define GPIO_PXDS2C_OFFSET	(0xE8)  /*  w, 32, 0x00000000 */
#define GPIO_PXSL_OFFSET	(0xF0)  /*  r, 32, 0x00000000 */
#define GPIO_PXSLS_OFFSET	(0xF4)  /*  w, 32, 0x00000000 */
#define GPIO_PXSLC_OFFSET	(0xF8)  /*  w, 32, 0x00000000 */

/*
 * GPIO registers address definition
 */
#define GPIO_PXPIN(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXPIN_OFFSET)
#define GPIO_PXDAT(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDAT_OFFSET)
#define GPIO_PXDATS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDATS_OFFSET)
#define GPIO_PXDATC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDATC_OFFSET)
#define GPIO_PXIM(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXIM_OFFSET)
#define GPIO_PXIMS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXIMS_OFFSET)
#define GPIO_PXIMC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXIMC_OFFSET)
#define GPIO_PXPE(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXPE_OFFSET)
#define GPIO_PXPES(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXPES_OFFSET)
#define GPIO_PXPEC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXPEC_OFFSET)
#define GPIO_PXFUN(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXFUN_OFFSET)
#define GPIO_PXFUNS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXFUNS_OFFSET)
#define GPIO_PXFUNC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXFUNC_OFFSET)
#define GPIO_PXSEL(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSEL_OFFSET)
#define GPIO_PXSELS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSELS_OFFSET)
#define GPIO_PXSELC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSELC_OFFSET)
#define GPIO_PXDIR(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDIR_OFFSET)
#define GPIO_PXDIRS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDIRS_OFFSET)
#define GPIO_PXDIRC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDIRC_OFFSET)
#define GPIO_PXTRG(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXTRG_OFFSET)
#define GPIO_PXTRGS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXTRGS_OFFSET)
#define GPIO_PXTRGC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXTRGC_OFFSET)
#define GPIO_PXFLG(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXFLG_OFFSET)
#define GPIO_PXFLGC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXFLGC_OFFSET)
#define GPIO_PXDS0(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS0_OFFSET)
#define GPIO_PXDS0S(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS0S_OFFSET)
#define GPIO_PXDS0C(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS0C_OFFSET)
#define GPIO_PXDS1(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS1_OFFSET)
#define GPIO_PXDS1S(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS1S_OFFSET)
#define GPIO_PXDS1C(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS1C_OFFSET)
#define GPIO_PXDS2(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS2_OFFSET)
#define GPIO_PXDS2S(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS2S_OFFSET)
#define GPIO_PXDS2C(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXDS2C_OFFSET)
#define GPIO_PXSL(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSL_OFFSET)
#define GPIO_PXSLS(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSLS_OFFSET)
#define GPIO_PXSLC(n)	(GPIO_BASE + (n)*GPIO_GOS + GPIO_PXSLC_OFFSET)

/*  */
#define GPIO_PORT_NUM   6
#define MAX_GPIO_NUM	192
#define GPIO_WAKEUP     (30)

#ifndef __MIPS_ASSEMBLER

//n = 0,1,2,3,4,5 (PORTA, PORTB, PORTC, PORTD, PORTE, PORTF)
#define REG_GPIO_PXPIN(n)	REG32(GPIO_PXPIN(n))  /* PIN Level Register */
#define REG_GPIO_PXDAT(n)	REG32(GPIO_PXDAT(n))  /* Port Data Register */
#define REG_GPIO_PXDATS(n)	REG32(GPIO_PXDATS(n)) /* Port Data Set Register */
#define REG_GPIO_PXDATC(n)	REG32(GPIO_PXDATC(n)) /* Port Data Clear Register */
#define REG_GPIO_PXIM(n)	REG32(GPIO_PXIM(n))   /* Interrupt Mask Register */
#define REG_GPIO_PXIMS(n)	REG32(GPIO_PXIMS(n))  /* Interrupt Mask Set Reg */
#define REG_GPIO_PXIMC(n)	REG32(GPIO_PXIMC(n))  /* Interrupt Mask Clear Reg */
#define REG_GPIO_PXPE(n)	REG32(GPIO_PXPE(n))   /* Pull Enable Register */
#define REG_GPIO_PXPES(n)	REG32(GPIO_PXPES(n))  /* Pull Enable Set Register */
#define REG_GPIO_PXPEC(n)	REG32(GPIO_PXPEC(n))  /* Pull Enable Clear Register */
#define REG_GPIO_PXFUN(n)	REG32(GPIO_PXFUN(n))  /* Function Register */
#define REG_GPIO_PXFUNS(n)	REG32(GPIO_PXFUNS(n)) /* Function Set Register */
#define REG_GPIO_PXFUNC(n)	REG32(GPIO_PXFUNC(n)) /* Function Clear Register */
#define REG_GPIO_PXSEL(n)	REG32(GPIO_PXSEL(n))  /* Select Register */
#define REG_GPIO_PXSELS(n)	REG32(GPIO_PXSELS(n)) /* Select Set Register */
#define REG_GPIO_PXSELC(n)	REG32(GPIO_PXSELC(n)) /* Select Clear Register */
#define REG_GPIO_PXDIR(n)	REG32(GPIO_PXDIR(n))  /* Direction Register */
#define REG_GPIO_PXDIRS(n)	REG32(GPIO_PXDIRS(n)) /* Direction Set Register */
#define REG_GPIO_PXDIRC(n)	REG32(GPIO_PXDIRC(n)) /* Direction Clear Register */
#define REG_GPIO_PXTRG(n)	REG32(GPIO_PXTRG(n))  /* Trigger Register */
#define REG_GPIO_PXTRGS(n)	REG32(GPIO_PXTRGS(n)) /* Trigger Set Register */
#define REG_GPIO_PXTRGC(n)	REG32(GPIO_PXTRGC(n)) /* Trigger Clear Register */
#define REG_GPIO_PXFLG(n)	REG32(GPIO_PXFLG(n))  /* Port Flag Register */
#define REG_GPIO_PXFLGC(n)	REG32(GPIO_PXFLGC(n)) /* Port Flag clear Register */
#define REG_GPIO_PXDS0(n) 	REG32(GPIO_PXDS0(n))  /* Port Drive Strength*/
#define REG_GPIO_PXDS0S(n) 	REG32(GPIO_PXDS0S(n))
#define REG_GPIO_PXDS0C(n) 	REG32(GPIO_PXDS0C(n))
#define REG_GPIO_PXDS1(n) 	REG32(GPIO_PXDS1(n))
#define REG_GPIO_PXDS1S(n) 	REG32(GPIO_PXDS1S(n))
#define REG_GPIO_PXDS1C(n) 	REG32(GPIO_PXDS1C(n))
#define REG_GPIO_PXDS2(n) 	REG32(GPIO_PXDS2(n))
#define REG_GPIO_PXDS2S(n) 	REG32(GPIO_PXDS2S(n))
#define REG_GPIO_PXDS2C(n) 	REG32(GPIO_PXDS2C(n))
#define REG_GPIO_PXSL(n) 	REG32(GPIO_PXSL(n))   /* Port Slew */
#define REG_GPIO_PXSLS(n) 	REG32(GPIO_PXSLS(n))  /* Port Slew -- Fast */
#define REG_GPIO_PXSLC(n) 	REG32(GPIO_PXSLC(n))  /* Port Slew -- Slow */

/*----------------------------------------------------------------
 * p is the port number (0,1,2,3,4,5)
 * o is the pin offset (0-31) inside the port
 * n is the absolute number of a pin (0-127), regardless of the port
 */

//----------------------------------------------------------------
// Function Pins Mode

#define __gpio_as_func0(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXFUNS(p) = (1 << o);		\
	REG_GPIO_PXTRGC(p) = (1 << o);		\
	REG_GPIO_PXSELC(p) = (1 << o);		\
} while (0)

#define __gpio_as_func1(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXFUNS(p) = (1 << o);		\
	REG_GPIO_PXTRGC(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
} while (0)

#define __gpio_as_func2(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXFUNS(p) = (1 << o);		\
	REG_GPIO_PXTRGS(p) = (1 << o);		\
	REG_GPIO_PXSELC(p) = (1 << o);		\
} while (0)

#define __gpio_as_func3(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXFUNS(p) = (1 << o);		\
	REG_GPIO_PXTRGS(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
} while (0)

/*
 * UART0_TxD, UART0_RxD
 */
#define __gpio_as_uart0()			\
do {						\
	unsigned int bits = BIT3 | BIT0;	\
	REG_GPIO_PXFUNS(5) = bits;		\
	REG_GPIO_PXTRGC(5) = bits;		\
	REG_GPIO_PXSELC(5) = bits;		\
	REG_GPIO_PXPES(5)  = bits;		\
} while (0)

/*
 * UART0_TxD, UART0_RxD, UART0_CTS, UART0_RTS
 */
#define __gpio_as_uart0_ctsrts()		\
do {						\
	unsigned int bits = BITS_H2L(3, 0);	\
	REG_GPIO_PXFUNS(5) = bits;		\
	REG_GPIO_PXTRGC(5) = bits;		\
	REG_GPIO_PXSELC(5) = bits;		\
	REG_GPIO_PXPES(5)  = bits;		\
} while (0)

/*
 * UART1_TxD, UART1_RxD
 */
#define __gpio_as_uart1()			\
do {						\
	unsigned int bits = BIT28 | BIT26;	\
	REG_GPIO_PXFUNS(3) = bits;		\
	REG_GPIO_PXTRGC(3) = bits;		\
	REG_GPIO_PXSELC(3) = bits;		\
	REG_GPIO_PXPES(3)  = bits;		\
} while (0)

/*
 * UART1_TxD, UART1_RxD, UART1_CTS, UART1_RTS
 */
#define __gpio_as_uart1_ctsrts()		\
do {						\
	unsigned int bits = BITS_H2L(29, 26);	\
	REG_GPIO_PXFUNS(3) = bits;		\
	REG_GPIO_PXTRGC(3) = bits;		\
	REG_GPIO_PXSELC(3) = bits;		\
	REG_GPIO_PXPES(3)  = bits;		\
} while (0)

/*
 * UART2_TxD, UART2_RxD
 */
#define __gpio_as_uart2()			\
do {						\
	unsigned int bits = BIT30 | BIT28;	\
	REG_GPIO_PXFUNS(2) = bits;		\
	REG_GPIO_PXTRGC(2) = bits;		\
	REG_GPIO_PXSELC(2) = bits;		\
	REG_GPIO_PXPES(2)  = bits;		\
} while (0)

/*
 * UART2_TxD, UART2_RxD, UART2_CTS, UART2_RTS
 */
#define __gpio_as_uart2_ctsrts()		\
do {						\
	unsigned int bits = BITS_H2L(31, 28);	\
	REG_GPIO_PXFUNS(2) = bits;		\
	REG_GPIO_PXTRGC(2) = bits;		\
	REG_GPIO_PXSELC(2) = bits;		\
	REG_GPIO_PXPES(2)  = bits;		\
} while (0)

/* WARNING: the folloing macro do NOT check */
/*
 * UART3_TxD, UART3_RxD
 */
#define __gpio_as_uart3()			\
do {						\
	unsigned int bits = BIT12;	\
	REG_GPIO_PXFUNS(3) = bits;		\
	REG_GPIO_PXTRGC(3) = bits;		\
	REG_GPIO_PXSELS(3) = bits;		\
	REG_GPIO_PXPES(3)  = bits;	\
	bits = BIT5;	\
	REG_GPIO_PXFUNS(4) = bits;		\
	REG_GPIO_PXTRGC(4) = bits;		\
	REG_GPIO_PXSELS(4) = bits;		\
	REG_GPIO_PXPES(4)  = bits;	\
} while (0)
/*
 * UART3_TxD, UART3_RxD, UART3_CTS, UART3_RTS
 */
#define __gpio_as_uart3_ctsrts()		\
do {						\
	REG_GPIO_PXFUNS(3) = (1 << 12);		\
	REG_GPIO_PXTRGC(3) = (1 << 12);		\
	REG_GPIO_PXSELC(3) = (1 << 12);		\
	REG_GPIO_PXPES(3)  = (1 << 12);		\
	REG_GPIO_PXFUNS(4) = 0x00000320;	\
	REG_GPIO_PXTRGC(4) = 0x00000320;	\
	REG_GPIO_PXSELS(4) = 0x00000020;	\
	REG_GPIO_PXSELC(4) = 0x00000300;	\
	REG_GPIO_PXPES(4)  = 0x00000320;	\
} while (0)

/*
 * SD0 ~ SD7, CS1#, CLE, ALE, FRE#, FWE#, FRB#
 * @n: chip select number(1 ~ 6)
 */
#define __gpio_as_nand_8bit(n)						\
do {		              						\
									\
	REG_GPIO_PXFUNS(0) = 0x000c00ff; /* SD0 ~ SD7, FRE#, FWE# */ \
	REG_GPIO_PXSELC(0) = 0x000c00ff;				\
	REG_GPIO_PXTRGC(0) = 0x000c00ff;				\
	REG_GPIO_PXPES(0) = 0x000c00ff;					\
	REG_GPIO_PXFUNS(1) = 0x00000003; /* CLE(SA0_CL), ALE(SA1_AL) */	\
	REG_GPIO_PXSELC(1) = 0x00000003;				\
	REG_GPIO_PXTRGC(1) = 0x00000003;				\
	REG_GPIO_PXPES(1) = 0x00000003;					\
									\
	REG_GPIO_PXFUNS(0) = 0x00200000 << ((n)-1); /* CSn */		\
	REG_GPIO_PXSELC(0) = 0x00200000 << ((n)-1);			\
	REG_GPIO_PXTRGC(0) = 0x00200000 << ((n)-1);			\
	REG_GPIO_PXPES(0) = 0x00200000 << ((n)-1);			\
									\
 	REG_GPIO_PXFUNC(0) = 0x00100000; /* FRB#(input) */		\
	REG_GPIO_PXSELC(0) = 0x00100000;				\
	REG_GPIO_PXDIRC(0) = 0x00100000;				\
	REG_GPIO_PXPES(0) = 0x00100000;					\
} while (0)

#define __gpio_as_nand_16bit(n)						\
do {		              						\
									\
	REG_GPIO_PXFUNS(0) = 0x000cffff; /* SD0 ~ SD15, CS1#, FRE#, FWE# */ \
	REG_GPIO_PXSELC(0) = 0x000cffff;				\
	REG_GPIO_PXTRGC(0) = 0x000cffff;				\
	REG_GPIO_PXPES(0) = 0x000cffff;					\
	REG_GPIO_PXFUNS(1) = 0x00000003; /* CLE(SA2), ALE(SA3) */	\
	REG_GPIO_PXSELC(1) = 0x00000003;				\
	REG_GPIO_PXTRGC(1) = 0x00000003;				\
	REG_GPIO_PXPES(1) = 0x00000003;					\
									\
	REG_GPIO_PXFUNS(0) = 0x00200000 << ((n)-1); /* CSn */		\
	REG_GPIO_PXSELC(0) = 0x00200000 << ((n)-1);			\
	REG_GPIO_PXTRGC(0) = 0x00200000 << ((n)-1);			\
	REG_GPIO_PXPES(0) = 0x00200000 << ((n)-1);			\
									\
 	REG_GPIO_PXFUNC(0) = 0x00100000; /* FRB#(input) */		\
	REG_GPIO_PXSELC(0) = 0x00100000;				\
	REG_GPIO_PXDIRC(0) = 0x00100000;				\
	REG_GPIO_PXPES(0) = 0x00100000;					\
} while (0)

/*
 *  SLCD
 */
#define __gpio_as_slcd_16bit() \
do {						\
	REG_GPIO_PXFUNS(2) = 0x03cff0fc;	\
	REG_GPIO_PXTRGC(2) = 0x03cff0fc;	\
	REG_GPIO_PXSELC(2) = 0x03cff0fc;    \
	REG_GPIO_PXPES(2) = 0x03cff0fc;    \
} while (0)

/*
 * LCD_R3~LCD_R7, LCD_G2~LCD_G7, LCD_B3~LCD_B7,
 * LCD_PCLK, LCD_HSYNC, LCD_VSYNC, LCD_DE
 */
#define __gpio_as_lcd_16bit()			\
do {						\
	REG_GPIO_PXFUNS(2) = 0x0f8ff3f8;	\
	REG_GPIO_PXTRGC(2) = 0x0f8ff3f8;	\
	REG_GPIO_PXSELC(2) = 0x0f8ff3f8;	\
	REG_GPIO_PXPES(2) = 0x0f8ff3f8;		\
} while (0)

/*
 * LCD_R2~LCD_R7, LCD_G2~LCD_G7, LCD_B2~LCD_B7,
 * LCD_PCLK, LCD_HSYNC, LCD_VSYNC, LCD_DE
 */
#define __gpio_as_lcd_18bit()			\
do {						\
	REG_GPIO_PXFUNS(2) = 0x0fcff3fc;	\
	REG_GPIO_PXTRGC(2) = 0x0fcff3fc;	\
	REG_GPIO_PXSELC(2) = 0x0fcff3fc;	\
	REG_GPIO_PXPES(2) = 0x0fcff3fc;		\
} while (0)

/*
 * LCD_R0~LCD_R7, LCD_G0~LCD_G7, LCD_B0~LCD_B7,
 * LCD_PCLK, LCD_HSYNC, LCD_VSYNC, LCD_DE
 */
#define __gpio_as_lcd_24bit()			\
do {						\
	REG_GPIO_PXFUNS(2) = 0x0fffffff;	\
	REG_GPIO_PXTRGC(2) = 0x0fffffff;	\
	REG_GPIO_PXSELC(2) = 0x0fffffff;	\
	REG_GPIO_PXPES(2) = 0x0fffffff;		\
} while (0)

/*
 * LCD_R0~LCD_R7, LCD_G0~LCD_G7, LCD_B0~LCD_B7,
 * LCD_PCLK, LCD_HSYNC, LCD_VSYNC, LCD_DE
 */
#define __gpio_clear_lcd_24bit()		\
do {						\
	REG_GPIO_PXFUNC(2) = 0x0fffffff;	\
	REG_GPIO_PXTRGC(2) = 0x0fffffff;	\
	REG_GPIO_PXSELC(2) = 0x0fffffff;	\
	REG_GPIO_PXDIRS(2) = 0x0fffffff;	\
	REG_GPIO_PXDATC(2) = 0x0fffffff;	\
	REG_GPIO_PXPES(2) = 0x0fffffff;		\
} while (0)

/* Set data pin driver strength v: 0~7 */
#define __gpio_set_lcd_data_driving_strength(v) \
do {						\
	unsigned int d;			\
	d = v & 0x1;				\
	if(d) REG_GPIO_PXDS0S(2) = 0x0ff3fcff;	\
	else REG_GPIO_PXDS0C(2) = 0x0ff3fcff;	\
	d = v & 0x2;				\
	if(d) REG_GPIO_PXDS1S(2) = 0x0ff3fcff;	\
	else REG_GPIO_PXDS1C(2) = 0x0ff3fcff;	\
	d = v & 0x4;				\
	if(d) REG_GPIO_PXDS2S(2) = 0x0ff3fcff;	\
	else REG_GPIO_PXDS2C(2) = 0x0ff3fcff;	\
} while(0)
/* Set HSYNC VSYNC DE driver strength v: 0~7 */
#define __gpio_set_lcd_sync_driving_strength(v) \
do {						\
	unsigned int d;				\
	d = v & 0x1;				\
	if(d) REG_GPIO_PXDS0S(2) = 0x000c0200;	\
	else REG_GPIO_PXDS0C(2) = 0x000c0200;	\
	d = v & 0x2;				\
	if(d) REG_GPIO_PXDS1S(2) = 0x000c0200;	\
	else REG_GPIO_PXDS1C(2) = 0x000c0200;	\
	d = v & 0x4;				\
	if(d) REG_GPIO_PXDS2S(2) = 0x000c0200;	\
	else REG_GPIO_PXDS2C(2) = 0x000c0200;	\
} while(0)
/* Set PCLK driver strength v: 0~7 */
#define __gpio_set_lcd_clk_driving_strength(v)	\
do {						\
	unsigned int d;				\
	d = v & 0x1;				\
	if(d) REG_GPIO_PXDS0S(2) = (1 << 8);	\
	else REG_GPIO_PXDS0C(2) = (1 << 8);	\
	d = v & 0x2;				\
	if(d) REG_GPIO_PXDS1S(2) = (1 << 8);	\
	else REG_GPIO_PXDS1C(2) = (1 << 8);	\
	d = v & 0x4;				\
	if(d) REG_GPIO_PXDS2S(2) = (1 << 8);	\
	else REG_GPIO_PXDS2C(2) = (1 << 8);	\
} while(0)

/* Set fast slew rate */
#define __gpio_set_lcd_data_fslew(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLS(p) = 0x0ff3fcff;		\
} while(0)

/* Set slow slew rate */
#define __gpio_set_lcd_data_sslew(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLC(p) = 0x0ff3fcff;		\
} while(0)

/* Set fast slew rate */
#define __gpio_set_lcd_sync_fslew(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLS(p) = 0x000c0200;		\
} while(0)

/* Set slow slew rate */
#define __gpio_set_lcd_sync_sslew(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLC(p) = 0x000c0200;		\
} while(0)

/* Set fast slew rate */
#define __gpio_set_lcd_pclk_fslew(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLS(p) = (1 << 8);		\
} while(0)

/* Set slow slew rate */
#define __gpio_set_lcd_pclk_sslew(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLC(p) = (1 << 8);		\
} while(0)

/*
 *  LCD_CLS, LCD_SPL, LCD_PS, LCD_REV
 */
#define __gpio_as_lcd_special()			\
do {						\
	REG_GPIO_PXFUNS(2) = 0x0fffffff;	\
	REG_GPIO_PXTRGC(2) = 0x0fffffff;	\
	REG_GPIO_PXSELC(2) = 0x0feffbfc;	\
	REG_GPIO_PXSELS(2) = 0x00100403;	\
	REG_GPIO_PXPES(2) = 0x0fffffff;		\
} while (0)

#define __gpio_as_epd()				\
do {						\
	REG_GPIO_PXFUNS(1) = 0x00011e00;	\
	REG_GPIO_PXTRGS(1) = 0x00011e00;	\
	REG_GPIO_PXSELS(1) = 0x00011e00;	\
	REG_GPIO_PXPES(1)  = 0x00011e00;	\
} while (0)
/*
 * CIM_D0~CIM_D7, CIM_MCLK, CIM_PCLK, CIM_VSYNC, CIM_HSYNC
 */
#define __gpio_as_cim()				\
do {						\
	REG_GPIO_PXFUNS(1) = 0x0003ffc0;	\
	REG_GPIO_PXTRGC(1) = 0x0003ffc0;	\
	REG_GPIO_PXSELC(1) = 0x0003ffc0;	\
	REG_GPIO_PXPES(1)  = 0x0003ffc0;	\
} while (0)

/*
 * SDATO, SDATI, BCLK, SYNC, SCLK_RSTN(gpio sepc) or
 * SDATA_OUT, SDATA_IN, BIT_CLK, SYNC, SCLK_RESET(aic spec)
 */
#define __gpio_as_aic()		\
do {					\
	REG_GPIO_PXFUNS(3) = 0x00003000;	\
	REG_GPIO_PXTRGC(3) = 0x00003000;	\
	REG_GPIO_PXSELS(3) = 0x00001000;	\
	REG_GPIO_PXSELC(3) = 0x00002000;	\
	REG_GPIO_PXPES(3)  = 0x00003000;	\
	REG_GPIO_PXFUNS(4) = 0x000000e0;	\
	REG_GPIO_PXTRGS(4) = 0x00000020;	\
	REG_GPIO_PXTRGC(4) = 0x000000c0;	\
	REG_GPIO_PXSELC(4) = 0x000000e0;	\
	REG_GPIO_PXPES(4)  = 0x000000e0;	\
} while (0)

#define __gpio_as_spdif()		\
do {					\
	REG_GPIO_PXFUNS(3) = 0x00003000;	\
	REG_GPIO_PXTRGC(3) = 0x00003000;	\
	REG_GPIO_PXSELS(3) = 0x00001000;	\
	REG_GPIO_PXSELC(3) = 0x00002000;	\
	REG_GPIO_PXPES(3)  = 0x00003000;	\
	REG_GPIO_PXFUNS(4) = 0x000038e0;	\
	REG_GPIO_PXTRGC(4) = 0x000038c0;	\
	REG_GPIO_PXTRGS(4) = 0x00000020;	\
	REG_GPIO_PXSELC(4) = 0x000038e0;	\
	REG_GPIO_PXPES(4)  = 0x000038e0;	\
} while (0)

/*
 * MSC0_CMD, MSC0_CLK, MSC0_D0 ~ MSC0_D3
 */
#define __gpio_as_msc0_pa_4bit()		\
do {						\
	REG_GPIO_PXFUNS(0) = 0x00fc0000;	\
	REG_GPIO_PXTRGC(0) = 0x00fc0000;	\
	REG_GPIO_PXSELS(0) = 0x00ec0000;	\
	REG_GPIO_PXSELC(0) = 0x00100000;	\
	REG_GPIO_PXPES(0)  = 0x00fc0000;	\
} while (0)

/*
 * MSC0_CMD, MSC0_CLK, MSC0_D0 ~ MSC0_D7
 */
#define __gpio_as_msc0_pe_8bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x3ff00000;	\
	REG_GPIO_PXTRGC(4) = 0x3ff00000;	\
	REG_GPIO_PXSELC(4) = 0x3ff00000;	\
	REG_GPIO_PXPES(4)  = 0x3ff00000;	\
	REG_GPIO_PXDS0S(4) = 0x3ff00000;        \
} while (0)
/*
 * MSC0_CMD, MSC0_CLK, MSC0_D0 ~ MSC0_D3
 */
#define __gpio_as_msc0_pe_4bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x30f00000;	\
	REG_GPIO_PXTRGC(4) = 0x30f00000;	\
	REG_GPIO_PXSELC(4) = 0x30f00000;	\
	REG_GPIO_PXPES(4)  = 0x30f00000;	\
	REG_GPIO_PXDS0S(4) = 0x30f00000;        \
} while (0)

#define __gpio_as_msc0_boot()			\
do {        					\
	REG_GPIO_PXFUNS(0) = 0x00ec0000;	\
	REG_GPIO_PXTRGC(0) = 0x00ec0000;	\
	REG_GPIO_PXSELS(0) = 0x00ec0000;	\
	REG_GPIO_PXPES(0)  = 0x00ec0000;	\
	REG_GPIO_PXFUNS(0) = 0x00100000;	\
	REG_GPIO_PXTRGC(0) = 0x00100000;	\
	REG_GPIO_PXSELC(0) = 0x00100000;	\
	REG_GPIO_PXPES(0)  = 0x00100000;	\
						\
} while (0)

/*
 * MSC1_CMD, MSC1_CLK, MSC1_D0 ~ MSC1_D7
 */
#define __gpio_as_msc1_pe_8bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x3ff00000;	\
	REG_GPIO_PXTRGC(4) = 0x3ff00000;	\
	REG_GPIO_PXSELS(4) = 0x3ff00000;	\
	REG_GPIO_PXPES(4)  = 0x3ff00000;	\
	REG_GPIO_PXDS0S(4) = 0x3ff00000;        \
} while (0)
/*
 * MSC1_CMD, MSC1_CLK, MSC1_D0 ~ MSC1_D3
 */
#define __gpio_as_msc1_pe_4bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x30f00000;	\
	REG_GPIO_PXTRGC(4) = 0x30f00000;	\
	REG_GPIO_PXSELS(4) = 0x30f00000;	\
	REG_GPIO_PXPES(4)  = 0x30f00000;	\
	REG_GPIO_PXDS0S(4) = 0x30f00000;        \
} while (0)

/*
 * MSC1_CMD, MSC1_CLK, MSC1_D0 ~ MSC1_D3
 */
#define __gpio_as_msc1_pd_4bit()			\
do {						\
	REG_GPIO_PXFUNS(3) = 0x03f00000;	\
	REG_GPIO_PXTRGC(3) = 0x03f00000;	\
	REG_GPIO_PXSELC(3) = 0x03f00000;	\
	REG_GPIO_PXPES(3)  = 0x03f00000;	\
	REG_GPIO_PXDS0S(3) = 0x03f00000;        \
} while (0)

/* Port B
 * MSC2_CMD, MSC2_CLK, MSC2_D0 ~ MSC2_D3
 */
#define __gpio_as_msc2_pb_4bit()		\
do {						\
	REG_GPIO_PXFUNS(1) = 0xf0300000;	\
	REG_GPIO_PXTRGC(1) = 0xf0300000;	\
	REG_GPIO_PXSELC(1) = 0xf0300000;	\
	REG_GPIO_PXPES(1)  = 0xf0300000;	\
	REG_GPIO_PXDS0S(1) = 0xf0300000;        \
} while (0)

/*
 * MSC2_CMD, MSC2_CLK, MSC2_D0 ~ MSC2_D7
 */
#define __gpio_as_msc2_pe_8bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x3ff00000;	\
	REG_GPIO_PXTRGS(4) = 0x3ff00000;	\
	REG_GPIO_PXSELC(4) = 0x3ff00000;	\
	REG_GPIO_PXPES(4)  = 0x3ff00000;	\
	REG_GPIO_PXDS0S(4) = 0x3ff00000;        \
} while (0)
/*
 * MSC2_CMD, MSC2_CLK, MSC2_D0 ~ MSC2_D3
 */
#define __gpio_as_msc2_pe_4bit()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x30f00000;	\
	REG_GPIO_PXTRGS(4) = 0x30f00000;	\
	REG_GPIO_PXSELC(4) = 0x30f00000;	\
	REG_GPIO_PXPES(4)  = 0x30f00000;	\
	REG_GPIO_PXDS0S(4)= 0x30f00000;		\
} while (0)
#define __gpio_as_msc0_4bit	__gpio_as_msc0_pe_4bit /* default as msc0 4bit */
#define __gpio_as_msc1_4bit	__gpio_as_msc1_pd_4bit /* msc1 only support 4bit */
#define __gpio_as_msc 		__gpio_as_msc0_4bit /* default as msc0 4bit */
#define __gpio_as_msc0 		__gpio_as_msc0_4bit /* msc0 default as 4bit */
#define __gpio_as_msc1 		__gpio_as_msc1_4bit /* msc1 only support 4bit */
#define __gpio_as_msc0_8bit	__gpio_as_msc0_pe_8bit /* default as msc0 8bit */
#define __gpio_as_msc1_8bit 	__gpio_as_msc1_pd_8bit /* msc1 only support 8bit */
/*
 * TSCLK, TSSTR, TSFRM, TSFAIL, TSDI0~7
 */
#define __gpio_as_tssi_1()			\
do {						\
	REG_GPIO_PXFUNS(1) = 0x0003ffc0;	\
	REG_GPIO_PXTRGC(1) = 0x0003ffc0;	\
	REG_GPIO_PXSELS(1) = 0x0003ffc0;	\
	REG_GPIO_PXPES(1)  = 0x0003ffc0;	\
} while (0)

/*
 * TSCLK, TSSTR, TSFRM, TSFAIL, TSDI0~7
 */
#define __gpio_as_tssi_2()			\
do {						\
	REG_GPIO_PXFUNS(1) = 0xfff00000;	\
	REG_GPIO_PXTRGC(1) = 0x0fc00000;	\
	REG_GPIO_PXTRGS(1) = 0xf0300000;	\
	REG_GPIO_PXSELC(1) = 0xfff00000;	\
	REG_GPIO_PXPES(1)  = 0xfff00000;	\
} while (0)

/*
 * SSI_CE0, SSI_CE1, SSI_GPC, SSI_CLK, SSI_DT, SSI_DR
 */
#define __gpio_as_ssi()				\
do {						\
	REG_GPIO_PXFUNS(0) = 0x002c0000; /* SSI0_CE0, SSI0_CLK, SSI0_DT	*/ \
	REG_GPIO_PXTRGS(0) = 0x002c0000;	\
	REG_GPIO_PXSELC(0) = 0x002c0000;	\
	REG_GPIO_PXPES(0)  = 0x002c0000;	\
						\
	REG_GPIO_PXFUNS(0) = 0x00100000; /* SSI0_DR */	\
	REG_GPIO_PXTRGC(0) = 0x00100000;	\
	REG_GPIO_PXSELS(0) = 0x00100000;	\
	REG_GPIO_PXPES(0)  = 0x00100000;	\
} while (0)

/*
 * SSI_CE0, SSI_CE2, SSI_GPC, SSI_CLK, SSI_DT, SSI1_DR
 */
#define __gpio_as_ssi_1()			\
do {						\
	REG_GPIO_PXFUNS(5) = 0x0000fc00;	\
	REG_GPIO_PXTRGC(5) = 0x0000fc00;	\
	REG_GPIO_PXSELC(5) = 0x0000fc00;	\
	REG_GPIO_PXPES(5)  = 0x0000fc00;	\
} while (0)

/* Port B
 * SSI2_CE0, SSI2_CE2, SSI2_GPC, SSI2_CLK, SSI2_DT, SSI12_DR
 */
#define __gpio_as_ssi2_1()			\
do {						\
	REG_GPIO_PXFUNS(5) = 0xf0300000;	\
	REG_GPIO_PXTRGC(5) = 0xf0300000;	\
	REG_GPIO_PXSELS(5) = 0xf0300000;	\
	REG_GPIO_PXPES(5)  = 0xf0300000;	\
} while (0)

#define __gpio_as_pcm0() \
do {						\
	REG_GPIO_PXFUNS(3) = 0xf;	\
	REG_GPIO_PXTRGC(3) = 0xf;	\
	REG_GPIO_PXSELC(3) = 0xf;	\
	REG_GPIO_PXPES(3)  = 0xf;	\
} while (0)

#define __gpio_as_pcm1() \
do {						\
	REG_GPIO_PXFUNS(3) = 0x1000;	\
	REG_GPIO_PXTRGS(3) = 0x1000;	\
	REG_GPIO_PXSELC(3) = 0x1000;	\
	REG_GPIO_PXPES(3)  = 0x1000;	\
	REG_GPIO_PXFUNS(4) = 0x1000;	\
	REG_GPIO_PXTRGC(4) = 0x300;	\
	REG_GPIO_PXTRGS(4) = 0x20;	\
	REG_GPIO_PXSELS(4) = 0x320;	\
	REG_GPIO_PXPES(4)  = 0x320;	\
} while (0)
/*
 * I2C_SCK, I2C_SDA
 */
#define __gpio_as_i2c(n)		       \
do {						\
	REG_GPIO_PXFUNS(3+(n)) = 0xc0000000;	\
	REG_GPIO_PXTRGC(3+(n)) = 0xc0000000;	\
	REG_GPIO_PXSELC(3+(n)) = 0xc0000000;	\
	REG_GPIO_PXPES(3+(n))  = 0xc0000000;	\
} while (0)

/*
 * PWM0
 */
#define __gpio_as_pwm0()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x1;	\
	REG_GPIO_PXTRGC(4) = 0x1;	\
	REG_GPIO_PXSELC(4) = 0x1;	\
	REG_GPIO_PXPES(4) = 0x1;		\
} while (0)

/*
 * PWM1
 */
#define __gpio_as_pwm1()			\
do {						\
	REG_GPIO_PXFUNS(4) = 0x2;	        \
	REG_GPIO_PXTRGC(4) = 0x2;		\
	REG_GPIO_PXSELC(4) = 0x2;		\
	REG_GPIO_PXPEC(4) = 0x2;		\
} while (0)

/*
 * PWM2
 */
#define __gpio_as_pwm2()		\
do {					\
	REG_GPIO_PXFUNS(4) = 0x4;	\
	REG_GPIO_PXTRGC(4) = 0x4;	\
	REG_GPIO_PXSELC(4) = 0x4;	\
	REG_GPIO_PXPES(4) = 0x4;	\
} while (0)

/*
 * PWM3
 */
#define __gpio_as_pwm3()		\
do {					\
	REG_GPIO_PXFUNS(4) = 0x8;	\
	REG_GPIO_PXTRGC(4) = 0x8;	\
	REG_GPIO_PXSELC(4) = 0x8;	\
	REG_GPIO_PXPES(4) = 0x8;	\
} while (0)

/*
 * PWM4
 */
#define __gpio_as_pwm4()		\
do {					\
	REG_GPIO_PXFUNS(4) = 0x10;	\
	REG_GPIO_PXTRGC(4) = 0x10;	\
	REG_GPIO_PXSELC(4) = 0x10;	\
	REG_GPIO_PXPES(4) = 0x10;	\
} while (0)

/*
 * PWM5
 */
#define __gpio_as_pwm5()		\
do {					\
	REG_GPIO_PXFUNS(4) = 0x20;	\
	REG_GPIO_PXTRGC(4) = 0x20;	\
	REG_GPIO_PXSELC(4) = 0x20;	\
	REG_GPIO_PXPES(4) = 0x20;	\
} while (0)

/*
 * n = 0 ~ 5
 */
#define __gpio_as_pwm(n)	__gpio_as_pwm##n()

/*
 * OWI - PA29 function 1
 */
#define __gpio_as_owi()				\
do {						\
	REG_GPIO_PXFUNS(0) = 0x20000000;	\
	REG_GPIO_PXTRGC(0) = 0x20000000;	\
	REG_GPIO_PXSELS(0) = 0x20000000;	\
} while (0)

/*
 * SCC - PD08 function 0
 *       PD09 function 0
 */
#define __gpio_as_scc()				\
do {						\
	REG_GPIO_PXFUNS(3) = 0xc0000300;	\
	REG_GPIO_PXTRGC(3) = 0xc0000300;	\
	REG_GPIO_PXSELC(3) = 0xc0000300;	\
} while (0)

#define __gpio_as_otg_drvvbus()	\
do {	\
	REG_GPIO_PXDATC(4) = (1 << 10);		\
	REG_GPIO_PXPEC(4) = (1 << 10);		\
	REG_GPIO_PXSELC(4) = (1 << 10);		\
	REG_GPIO_PXTRGC(4) = (1 << 10);		\
	REG_GPIO_PXFUNS(4) = (1 << 10);		\
} while (0)

//-------------------------------------------
// GPIO or Interrupt Mode

#define __gpio_get_port(p)	(REG_GPIO_PXPIN(p))

#define __gpio_port_as_output(p, o)		\
do {						\
    REG_GPIO_PXFUNC(p) = (1 << (o));		\
    REG_GPIO_PXSELC(p) = (1 << (o));		\
    REG_GPIO_PXDIRS(p) = (1 << (o));		\
    REG_GPIO_PXPES(p) = (1 << (o));		\
} while (0)

#define __gpio_port_as_input(p, o)		\
do {						\
    REG_GPIO_PXFUNC(p) = (1 << (o));		\
    REG_GPIO_PXSELC(p) = (1 << (o));		\
    REG_GPIO_PXDIRC(p) = (1 << (o));		\
} while (0)

#define __gpio_as_output(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	__gpio_port_as_output(p, o);		\
} while (0)

#define __gpio_as_input(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	__gpio_port_as_input(p, o);		\
} while (0)

#define __gpio_set_pin(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXDATS(p) = (1 << o);		\
} while (0)

#define __gpio_clear_pin(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXDATC(p) = (1 << o);		\
} while (0)

#define __gpio_get_pin(n)			\
({						\
	unsigned int p, o, v;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	if (__gpio_get_port(p) & (1 << o))	\
		v = 1;				\
	else					\
		v = 0;				\
	v;					\
})

#define __gpio_as_irq_high_level(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMS(p) = (1 << o);		\
	REG_GPIO_PXTRGC(p) = (1 << o);		\
	REG_GPIO_PXFUNC(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
	REG_GPIO_PXDIRS(p) = (1 << o);		\
	REG_GPIO_PXFLGC(p) = (1 << o);		\
	REG_GPIO_PXIMC(p) = (1 << o);		\
} while (0)

#define __gpio_as_irq_low_level(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMS(p) = (1 << o);		\
	REG_GPIO_PXTRGC(p) = (1 << o);		\
	REG_GPIO_PXFUNC(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
	REG_GPIO_PXDIRC(p) = (1 << o);		\
	REG_GPIO_PXFLGC(p) = (1 << o);		\
	REG_GPIO_PXIMC(p) = (1 << o);		\
} while (0)

#define __gpio_as_irq_rise_edge(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMS(p) = (1 << o);		\
	REG_GPIO_PXTRGS(p) = (1 << o);		\
	REG_GPIO_PXFUNC(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
	REG_GPIO_PXDIRS(p) = (1 << o);		\
	REG_GPIO_PXFLGC(p) = (1 << o);		\
	REG_GPIO_PXIMC(p) = (1 << o);		\
} while (0)

#define __gpio_as_irq_fall_edge(n)		\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMS(p) = (1 << o);		\
	REG_GPIO_PXTRGS(p) = (1 << o);		\
	REG_GPIO_PXFUNC(p) = (1 << o);		\
	REG_GPIO_PXSELS(p) = (1 << o);		\
	REG_GPIO_PXDIRC(p) = (1 << o);		\
	REG_GPIO_PXFLGC(p) = (1 << o);		\
	REG_GPIO_PXIMC(p) = (1 << o);		\
} while (0)

#define __gpio_mask_irq(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMS(p) = (1 << o);		\
} while (0)

#define __gpio_unmask_irq(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXIMC(p) = (1 << o);		\
} while (0)

#define __gpio_ack_irq(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXFLGC(p) = (1 << o);		\
} while (0)

#define __gpio_get_irq()			\
({						\
	unsigned int tmp, v = 0;		\
	for (int p = 5; p >= 0; p--) {		\
		tmp = REG_GPIO_PXFLG(p);	\
		for (int i = 0; i < 32; i++)	\
			if (tmp & (1 << i))	\
				v = (32*p + i);	\
	}					\
	v;					\
})

#define __gpio_group_irq(n)			\
({						\
	register int tmp, i;			\
	tmp = REG_GPIO_PXFLG(n) & (~REG_GPIO_PXIM(n));	\
	for (i=31;i>=0;i--)			\
		if (tmp & (1 << i))		\
			break;			\
	i;					\
})

#define __gpio_enable_pull(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXPEC(p) = (1 << o);		\
} while (0)

#define __gpio_disable_pull(n)			\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXPES(p) = (1 << o);		\
} while (0)

/* Set pin driver strength v: 0~7 */
#define __gpio_set_driving_strength(n, v)	\
do {						\
	unsigned int p, o, d;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	d = v & 0x1;				\
	if(d) REG_GPIO_PXDS0S(p) = (1 << o);	\
	else REG_GPIO_PXDS0C(p) = (1 << o);	\
	d = v & 0x2;				\
	if(d) REG_GPIO_PXDS1S(p) = (1 << o);	\
	else REG_GPIO_PXDS1C(p) = (1 << o);	\
	d = v & 0x4;				\
	if(d) REG_GPIO_PXDS2S(p) = (1 << o);	\
	else REG_GPIO_PXDS2C(p) = (1 << o);	\
} while(0)

/* Set fast slew rate */
#define __gpio_set_fslew(n)	\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLS(p) = (1 << o);	\
} while(0)

/* Set slow slew rate */
#define __gpio_set_sslew(n)	\
do {						\
	unsigned int p, o;			\
	p = (n) / 32;				\
	o = (n) % 32;				\
	REG_GPIO_PXSLC(p) = (1 << o);	\
} while(0)

#endif /* __MIPS_ASSEMBLER */

#define DMAC_BASE	0xB3420000

/*************************************************************************
 * DMAC (DMA Controller)
 *************************************************************************/

#define MAX_DMA_NUM 	12  /* max 12 channels */
#define MAX_MDMA_NUM    3   /* max 3  channels */
#define MAX_BDMA_NUM	3   /* max 3  channels */
#define HALF_DMA_NUM	6   /* the number of one dma controller's channels */

/* m is the DMA controller index (0, 1), n is the DMA channel index (0 - 11) */

#define DMAC_DSAR(n)  (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x00 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA source address */
#define DMAC_DTAR(n)  (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x04 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA target address */
#define DMAC_DTCR(n)  (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x08 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA transfer count */
#define DMAC_DRSR(n)  (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x0c + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA request source */
#define DMAC_DCCSR(n) (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x10 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA control/status */
#define DMAC_DCMD(n)  (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x14 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA command */
#define DMAC_DDA(n)   (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0x18 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x20)) /* DMA descriptor address */
#define DMAC_DSD(n)   (DMAC_BASE + ((n)/HALF_DMA_NUM*0x100 + 0xc0 + ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM) * 0x04)) /* DMA Stride Address */

#define DMAC_DMACR(m)	(DMAC_BASE + 0x0300 + 0x100 * (m))              /* DMA control register */
#define DMAC_DMAIPR(m)	(DMAC_BASE + 0x0304 + 0x100 * (m))              /* DMA interrupt pending */
#define DMAC_DMADBR(m)	(DMAC_BASE + 0x0308 + 0x100 * (m))              /* DMA doorbell */
#define DMAC_DMADBSR(m)	(DMAC_BASE + 0x030C + 0x100 * (m))              /* DMA doorbell set */
#define DMAC_DMACKE(m)  (DMAC_BASE + 0x0310 + 0x100 * (m))
#define DMAC_DMACKES(m) (DMAC_BASE + 0x0314 + 0x100 * (m))
#define DMAC_DMACKEC(m) (DMAC_BASE + 0x0318 + 0x100 * (m))

#define REG_DMAC_DSAR(n)	REG32(DMAC_DSAR((n)))
#define REG_DMAC_DTAR(n)	REG32(DMAC_DTAR((n)))
#define REG_DMAC_DTCR(n)	REG32(DMAC_DTCR((n)))
#define REG_DMAC_DRSR(n)	REG32(DMAC_DRSR((n)))
#define REG_DMAC_DCCSR(n)	REG32(DMAC_DCCSR((n)))
#define REG_DMAC_DCMD(n)	REG32(DMAC_DCMD((n)))
#define REG_DMAC_DDA(n)		REG32(DMAC_DDA((n)))
#define REG_DMAC_DSD(n)         REG32(DMAC_DSD(n))
#define REG_DMAC_DMACR(m)	REG32(DMAC_DMACR(m))
#define REG_DMAC_DMAIPR(m)	REG32(DMAC_DMAIPR(m))
#define REG_DMAC_DMADBR(m)	REG32(DMAC_DMADBR(m))
#define REG_DMAC_DMADBSR(m)	REG32(DMAC_DMADBSR(m))
#define REG_DMAC_DMACKE(m)      REG32(DMAC_DMACKE(m))
#define REG_DMAC_DMACKES(m)     REG32(DMAC_DMACKES(m))
#define REG_DMAC_DMACKEC(m)     REG32(DMAC_DMACKEC(m))

// DMA request source register
#define DMAC_DRSR_RS_BIT	0
#define DMAC_DRSR_RS_MASK	(0x3f << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_NAND	(1 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_BCH_ENC	(2 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_BCH_DEC	(3 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_AUTO	(0x08 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_TSSIIN	(0x09 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_EXT      (0x0c << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART3OUT	(0x0e << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART3IN	(0x0f << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART2OUT	(0x10 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART2IN	(0x11 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART1OUT	(0x12 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART1IN	(0x13 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART0OUT	(0x14 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_UART0IN	(0x15 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_SSI0OUT	(0x16 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_SSI0IN	(0x17 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_AICOUT	(0x18 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_AICIN	(0x19 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC0OUT	(0x1a << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC0IN	(0x1b << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_TCU	(0x1c << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_SADC	(0x1d << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC1OUT	(0x1e << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC1IN	(0x1f << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_SSI1OUT	(0x20 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_SSI1IN	(0x21 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_PMOUT	(0x22 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_PMIN	(0x23 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC2OUT	(0x24 << DMAC_DRSR_RS_BIT)
  #define DMAC_DRSR_RS_MSC2IN	(0x25 << DMAC_DRSR_RS_BIT)

// DMA channel control/status register
#define DMAC_DCCSR_NDES		(1 << 31) /* descriptor (0) or not (1) ? */
#define DMAC_DCCSR_DES8    	(1 << 30) /* Descriptor 8 Word */
#define DMAC_DCCSR_DES4    	(0 << 30) /* Descriptor 4 Word */
#define DMAC_DCCSR_CDOA_BIT	16        /* copy of DMA offset address */
#define DMAC_DCCSR_CDOA_MASK	(0xff << DMAC_DCCSR_CDOA_BIT)

#define DMAC_DCCSR_AR		(1 << 4)  /* address error */
#define DMAC_DCCSR_TT		(1 << 3)  /* transfer terminated */
#define DMAC_DCCSR_HLT		(1 << 2)  /* DMA halted */
#define DMAC_DCCSR_CT		(1 << 1)  /* count terminated */
#define DMAC_DCCSR_EN		(1 << 0)  /* channel enable bit */

// DMA channel command register
#define DMAC_DCMD_EACKS_LOW  	(1 << 31) /* External DACK Output Level Select, active low */
#define DMAC_DCMD_EACKS_HIGH  	(0 << 31) /* External DACK Output Level Select, active high */
#define DMAC_DCMD_EACKM_WRITE 	(1 << 30) /* External DACK Output Mode Select, output in write cycle */
#define DMAC_DCMD_EACKM_READ 	(0 << 30) /* External DACK Output Mode Select, output in read cycle */
#define DMAC_DCMD_ERDM_BIT      28        /* External DREQ Detection Mode Select */
#define DMAC_DCMD_ERDM_MASK     (0x03 << DMAC_DCMD_ERDM_BIT)
  #define DMAC_DCMD_ERDM_LOW    (0 << DMAC_DCMD_ERDM_BIT)
  #define DMAC_DCMD_ERDM_FALL   (1 << DMAC_DCMD_ERDM_BIT)
  #define DMAC_DCMD_ERDM_HIGH   (2 << DMAC_DCMD_ERDM_BIT)
  #define DMAC_DCMD_ERDM_RISE   (3 << DMAC_DCMD_ERDM_BIT)
#define DMAC_DCMD_BLAST		(1 << 25) /* BCH last */
#define DMAC_DCMD_SAI		(1 << 23) /* source address increment */
#define DMAC_DCMD_DAI		(1 << 22) /* dest address increment */
#define DMAC_DCMD_RDIL_BIT	16        /* request detection interval length */
#define DMAC_DCMD_RDIL_MASK	(0x0f << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_IGN	(0 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_2	(0x01 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_4	(0x02 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_8	(0x03 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_12	(0x04 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_16	(0x05 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_20	(0x06 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_24	(0x07 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_28	(0x08 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_32	(0x09 << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_48	(0x0a << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_60	(0x0b << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_64	(0x0c << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_124	(0x0d << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_128	(0x0e << DMAC_DCMD_RDIL_BIT)
  #define DMAC_DCMD_RDIL_200	(0x0f << DMAC_DCMD_RDIL_BIT)
#define DMAC_DCMD_SWDH_BIT	14  /* source port width */
#define DMAC_DCMD_SWDH_MASK	(0x03 << DMAC_DCMD_SWDH_BIT)
  #define DMAC_DCMD_SWDH_32	(0 << DMAC_DCMD_SWDH_BIT)
  #define DMAC_DCMD_SWDH_8	(1 << DMAC_DCMD_SWDH_BIT)
  #define DMAC_DCMD_SWDH_16	(2 << DMAC_DCMD_SWDH_BIT)
#define DMAC_DCMD_DWDH_BIT	12  /* dest port width */
#define DMAC_DCMD_DWDH_MASK	(0x03 << DMAC_DCMD_DWDH_BIT)
  #define DMAC_DCMD_DWDH_32	(0 << DMAC_DCMD_DWDH_BIT)
  #define DMAC_DCMD_DWDH_8	(1 << DMAC_DCMD_DWDH_BIT)
  #define DMAC_DCMD_DWDH_16	(2 << DMAC_DCMD_DWDH_BIT)
#define DMAC_DCMD_DS_BIT	8  /* transfer data size of a data unit */
#define DMAC_DCMD_DS_MASK	(0x07 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_32BIT	(0 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_8BIT	(1 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_16BIT	(2 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_16BYTE	(3 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_32BYTE	(4 << DMAC_DCMD_DS_BIT)
  #define DMAC_DCMD_DS_64BYTE	(5 << DMAC_DCMD_DS_BIT)

#define DMAC_DCMD_STDE	        (1 << 2)  /* Stride enable */
#define DMAC_DCMD_TIE		(1 << 1)  /* DMA transfer interrupt enable */
#define DMAC_DCMD_LINK		(1 << 0)  /* descriptor link enable */

// DMA descriptor address register
#define DMAC_DDA_BASE_BIT	12  /* descriptor base address */
#define DMAC_DDA_BASE_MASK	(0x0fffff << DMAC_DDA_BASE_BIT)
#define DMAC_DDA_OFFSET_BIT	4   /* descriptor offset address */
#define DMAC_DDA_OFFSET_MASK	(0x0ff << DMAC_DDA_OFFSET_BIT)

// DMA stride address register
#define DMAC_DSD_TSD_BIT        16  /* target stride address */
#define DMAC_DSD_TSD_MASK      	(0xffff << DMAC_DSD_TSD_BIT)
#define DMAC_DSD_SSD_BIT        0  /* source stride address */
#define DMAC_DSD_SSD_MASK      	(0xffff << DMAC_DSD_SSD_BIT)

// DMA control register
#define DMAC_DMACR_FMSC		(1 << 31)  /* MSC Fast DMA mode */
#define DMAC_DMACR_FSSI		(1 << 30)  /* SSI Fast DMA mode */
#define DMAC_DMACR_FTSSI	(1 << 29)  /* TSSI Fast DMA mode */
#define DMAC_DMACR_FUART	(1 << 28)  /* UART Fast DMA mode */
#define DMAC_DMACR_FAIC		(1 << 27)  /* AIC Fast DMA mode */
#define DMAC_DMACR_PR_BIT	8  /* channel priority mode */
#define DMAC_DMACR_PR_MASK	(0x03 << DMAC_DMACR_PR_BIT)
  #define DMAC_DMACR_PR_012345	(0 << DMAC_DMACR_PR_BIT)
  #define DMAC_DMACR_PR_120345	(1 << DMAC_DMACR_PR_BIT)
  #define DMAC_DMACR_PR_230145	(2 << DMAC_DMACR_PR_BIT)
  #define DMAC_DMACR_PR_340125	(3 << DMAC_DMACR_PR_BIT)
#define DMAC_DMACR_HLT		(1 << 3)  /* DMA halt flag */
#define DMAC_DMACR_AR		(1 << 2)  /* address error flag */
#define DMAC_DMACR_DMAE		(1 << 0)  /* DMA enable bit */

// DMA doorbell register
#define DMAC_DMADBR_DB5		(1 << 5)  /* doorbell for channel 5 */
#define DMAC_DMADBR_DB4		(1 << 4)  /* doorbell for channel 4 */
#define DMAC_DMADBR_DB3		(1 << 3)  /* doorbell for channel 3 */
#define DMAC_DMADBR_DB2		(1 << 2)  /* doorbell for channel 2 */
#define DMAC_DMADBR_DB1		(1 << 1)  /* doorbell for channel 1 */
#define DMAC_DMADBR_DB0		(1 << 0)  /* doorbell for channel 0 */

// DMA doorbell set register
#define DMAC_DMADBSR_DBS5	(1 << 5)  /* enable doorbell for channel 5 */
#define DMAC_DMADBSR_DBS4	(1 << 4)  /* enable doorbell for channel 4 */
#define DMAC_DMADBSR_DBS3	(1 << 3)  /* enable doorbell for channel 3 */
#define DMAC_DMADBSR_DBS2	(1 << 2)  /* enable doorbell for channel 2 */
#define DMAC_DMADBSR_DBS1	(1 << 1)  /* enable doorbell for channel 1 */
#define DMAC_DMADBSR_DBS0	(1 << 0)  /* enable doorbell for channel 0 */

// DMA interrupt pending register
#define DMAC_DMAIPR_CIRQ5	(1 << 5)  /* irq pending status for channel 5 */
#define DMAC_DMAIPR_CIRQ4	(1 << 4)  /* irq pending status for channel 4 */
#define DMAC_DMAIPR_CIRQ3	(1 << 3)  /* irq pending status for channel 3 */
#define DMAC_DMAIPR_CIRQ2	(1 << 2)  /* irq pending status for channel 2 */
#define DMAC_DMAIPR_CIRQ1	(1 << 1)  /* irq pending status for channel 1 */
#define DMAC_DMAIPR_CIRQ0	(1 << 0)  /* irq pending status for channel 0 */

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * DMAC
 ***************************************************************************/

/* m is the DMA controller index (0, 1), n is the DMA channel index (0 - 11) */

#define __dmac_enable_module(m) \
	( REG_DMAC_DMACR(m) |= DMAC_DMACR_DMAE | DMAC_DMACR_PR_012345 )
#define __dmac_disable_module(m) \
	( REG_DMAC_DMACR(m) &= ~DMAC_DMACR_DMAE )

/* p=0,1,2,3 */
#define __dmac_set_priority(m,p)			\
do {							\
	REG_DMAC_DMACR(m) &= ~DMAC_DMACR_PR_MASK;	\
	REG_DMAC_DMACR(m) |= ((p) << DMAC_DMACR_PR_BIT);	\
} while (0)

#define __dmac_test_halt_error(m) ( REG_DMAC_DMACR(m) & DMAC_DMACR_HLT )
#define __dmac_test_addr_error(m) ( REG_DMAC_DMACR(m) & DMAC_DMACR_AR )

#define __dmac_channel_enable_clk(n) \
	REG_DMAC_DMACKES((n)/HALF_DMA_NUM) = 1 << ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM);

#define __dmac_channel_disable_clk(n) \
	REG_DMAC_DMACKEC((n)/HALF_DMA_NUM) = 1 << ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM);

#define __dmac_enable_descriptor(n) \
  ( REG_DMAC_DCCSR((n)) &= ~DMAC_DCCSR_NDES )
#define __dmac_disable_descriptor(n) \
  ( REG_DMAC_DCCSR((n)) |= DMAC_DCCSR_NDES )

#define __dmac_enable_channel(n)                 \
do {                                             \
	REG_DMAC_DCCSR((n)) |= DMAC_DCCSR_EN;    \
} while (0)
#define __dmac_disable_channel(n)                \
do {                                             \
	REG_DMAC_DCCSR((n)) &= ~DMAC_DCCSR_EN;   \
} while (0)
#define __dmac_channel_enabled(n) \
  ( REG_DMAC_DCCSR((n)) & DMAC_DCCSR_EN )

#define __dmac_channel_enable_irq(n) \
  ( REG_DMAC_DCMD((n)) |= DMAC_DCMD_TIE )
#define __dmac_channel_disable_irq(n) \
  ( REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_TIE )

#define __dmac_channel_transmit_halt_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_HLT )
#define __dmac_channel_transmit_end_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_TT )
#define __dmac_channel_address_error_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_AR )

#define __dmac_channel_count_terminated_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_CT )
#define __dmac_channel_descriptor_invalid_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_INV )

#define __dmac_channel_clear_transmit_halt(n)				\
	do {								\
		/* clear both channel halt error and globle halt error */ \
		REG_DMAC_DCCSR(n) &= ~DMAC_DCCSR_HLT;			\
		REG_DMAC_DMACR(n/HALF_DMA_NUM) &= ~DMAC_DMACR_HLT;	\
	} while (0)
#define __dmac_channel_clear_transmit_end(n) \
  (  REG_DMAC_DCCSR(n) &= ~DMAC_DCCSR_TT )
#define __dmac_channel_clear_address_error(n)				\
	do {								\
		REG_DMAC_DDA(n) = 0; /* clear descriptor address register */ \
		REG_DMAC_DSAR(n) = 0; /* clear source address register */ \
		REG_DMAC_DTAR(n) = 0; /* clear target address register */ \
		/* clear both channel addr error and globle address error */ \
		REG_DMAC_DCCSR(n) &= ~DMAC_DCCSR_AR;			\
		REG_DMAC_DMACR(n/HALF_DMA_NUM) &= ~DMAC_DMACR_AR;	\
	} while (0)

#define __dmac_channel_clear_count_terminated(n) \
  (  REG_DMAC_DCCSR((n)) &= ~DMAC_DCCSR_CT )
#define __dmac_channel_clear_descriptor_invalid(n) \
  (  REG_DMAC_DCCSR((n)) &= ~DMAC_DCCSR_INV )

#define __dmac_channel_set_transfer_unit_32bit(n)	\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_DMAC_DCMD((n)) |= MAC_DCMD_DS_32BIT;	\
} while (0)

#define __dmac_channel_set_transfer_unit_16bit(n)	\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_DS_16BIT;	\
} while (0)

#define __dmac_channel_set_transfer_unit_8bit(n)	\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_DS_8BIT;	\
} while (0)

#define __dmac_channel_set_transfer_unit_16byte(n)	\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_DS_16BYTE;	\
} while (0)

#define __dmac_channel_set_transfer_unit_32byte(n)	\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_DS_32BYTE;	\
} while (0)

/* w=8,16,32 */
#define __dmac_channel_set_dest_port_width(n,w)		\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DWDH_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_DWDH_##w;	\
} while (0)

/* w=8,16,32 */
#define __dmac_channel_set_src_port_width(n,w)		\
do {							\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_SWDH_MASK;	\
	REG_DMAC_DCMD((n)) |= DMAC_DCMD_SWDH_##w;	\
} while (0)

/* v=0-15 */
#define __dmac_channel_set_rdil(n,v)				\
do {								\
	REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_RDIL_MASK;		\
	REG_DMAC_DCMD((n) |= ((v) << DMAC_DCMD_RDIL_BIT);	\
} while (0)

#define __dmac_channel_dest_addr_fixed(n) \
  (  REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_DAI )
#define __dmac_channel_dest_addr_increment(n) \
  (  REG_DMAC_DCMD((n)) |= DMAC_DCMD_DAI )

#define __dmac_channel_src_addr_fixed(n) \
  (  REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_SAI )
#define __dmac_channel_src_addr_increment(n) \
  (  REG_DMAC_DCMD((n)) |= DMAC_DCMD_SAI )

#define __dmac_channel_set_doorbell(n)	\
	(  REG_DMAC_DMADBSR((n)/HALF_DMA_NUM) = (1 << ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM)) )

#define __dmac_channel_irq_detected(n)  ( REG_DMAC_DMAIPR((n)/HALF_DMA_NUM) & (1 << ((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM)) )
#define __dmac_channel_ack_irq(n)       ( REG_DMAC_DMAIPR((n)/HALF_DMA_NUM) &= ~(1 <<((n)-(n)/HALF_DMA_NUM*HALF_DMA_NUM)) )

static __inline__ int __dmac_get_irq(void)
{
	int i;
	for (i = 0; i < MAX_DMA_NUM; i++)
		if (__dmac_channel_irq_detected(i))
			return i;
	return -1;
}

#endif /* __MIPS_ASSEMBLER */

/*
 * Interrupt controller module(INTC) address definition
 */
#define	INTC_BASE	0xB0001000

/*
 * INTC registers offset address definition
 */
#define INTC_ICSR_OFFSET	(0x00)	/* 32, r,  0x00000000 */
#define INTC_ICMR_OFFSET	(0x04)	/* 32, rw, 0xffffffff */
#define INTC_ICMSR_OFFSET	(0x08)	/* 32, w,  0x???????? */
#define INTC_ICMCR_OFFSET	(0x0c)	/* 32, w,  0x???????? */
#define INTC_ICPR_OFFSET	(0x10)	/* 32, r,  0x00000000 */

/* INTC groups offset */
#define INTC_GOS		0x20

/*
 * INTC registers address definition
 */
#define INTC_ICSR(n)	(INTC_BASE + (n) * INTC_GOS + INTC_ICSR_OFFSET)
#define INTC_ICMR(n)	(INTC_BASE + (n) * INTC_GOS + INTC_ICMR_OFFSET)
#define INTC_ICMSR(n)	(INTC_BASE + (n) * INTC_GOS + INTC_ICMSR_OFFSET)
#define INTC_ICMCR(n)	(INTC_BASE + (n) * INTC_GOS + INTC_ICMCR_OFFSET)
#define INTC_ICPR(n)	(INTC_BASE + (n) * INTC_GOS + INTC_ICPR_OFFSET)

/*
 * INTC registers common define
 */

/* 1st-level interrupts */
#define IRQ_I2C1	0
#define IRQ_I2C0	1
#define IRQ_UART3	2
#define IRQ_UART2	3
#define IRQ_UART1	4
#define IRQ_UART0	5
#define IRQ_GPU		6
#define IRQ_SSI1	7
#define IRQ_SSI0	8
#define IRQ_TSSI	9
#define IRQ_BDMA	10
#define IRQ_KBC		11
#define IRQ_GPIO5	12
#define IRQ_GPIO4	13
#define IRQ_GPIO3	14
#define IRQ_GPIO2	15
#define IRQ_GPIO1	16
#define IRQ_GPIO0	17
#define IRQ_SADC	18
#define IRQ_ETH		19
#define IRQ_UHC		20
#define IRQ_OTG		21
#define IRQ_MDMA	22
#define IRQ_DMAC1	23
#define IRQ_DMAC0	24
#define IRQ_TCU2	25
#define IRQ_TCU1	26
#define IRQ_TCU0	27
#define IRQ_GPS		28
#define IRQ_IPU		29
#define IRQ_CIM		30
#define IRQ_LCD		31

#define IRQ_RTC		32	/* 32 + 0 */
#define IRQ_OWI		33	/* 32 + 1 */
#define IRQ_AIC 	34	/* 32 + 2 */
#define IRQ_MSC2	35	/* 32 + 3 */
#define IRQ_MSC1	36	/* 32 + 4 */
#define IRQ_MSC0	37	/* 32 + 5 */
#define IRQ_SCC		38	/* 32 + 6 */
#define IRQ_BCH		39	/* 32 + 7 */
#define IRQ_PCM		40	/* 32 + 8 */
#define IRQ_HARB0	41	/* 32 + 9 */
#define IRQ_HARB2	42	/* 32 + 10 */
#define IRQ_AOSD	43	/* 32 + 11 */
#define IRQ_CPM		44	/* 32 + 12 */

#define IRQ_INTC_MAX	45

/* 2nd-level interrupts */
#define IRQ_DMA_BASE	(IRQ_INTC_MAX)
#define IRQ_DMA_MAX	(IRQ_DMA_BASE + NUM_DMA)

#define IRQ_MDMA_BASE	(IRQ_DMA_MAX)
#define IRQ_MDMA_MAX	(IRQ_MDMA_BASE + NUM_MDMA)

#define IRQ_BDMA_BASE	(IRQ_MDMA_MAX)
#define IRQ_BDMA_MAX	(IRQ_BDMA_BASE + NUM_BDMA)

/* To be cleanup begin */
#define IRQ_DMA_0	46
#define IRQ_DMA_1	(IRQ_DMA_0 + HALF_DMA_NUM)	/* 46 +  6 = 52 */
#define IRQ_MDMA_0	(IRQ_DMA_0 + MAX_DMA_NUM)	/* 46 + 12 = 58 */
#define IRQ_BDMA_0	(IRQ_DMA_0 + MAX_DMA_NUM + MAX_MDMA_NUM)	/* 46 + 12 + 2 = 60 */

#define IRQ_GPIO_0		64  /* 64 to (64+MAX_GPIO_NUM-1) for GPIO pin 0 to MAX_GPIO_NUM-1 */

#define NUM_INTC		45
#define NUM_DMA         	MAX_DMA_NUM
#define NUM_MDMA		MAX_MDMA_NUM
#define NUM_BDMA		MAX_BDMA_NUM
#define NUM_GPIO        	MAX_GPIO_NUM
/* To be cleanup end */

#ifndef __MIPS_ASSEMBLER

#define REG_INTC_ICMR(n)	REG32(INTC_ICMR(n))
#define REG_INTC_ICMSR(n)	REG32(INTC_ICMSR(n))
#define REG_INTC_ICMCR(n)	REG32(INTC_ICMCR(n))
#define REG_INTC_ICPR(n)	REG32(INTC_ICPR(n))

#define __intc_unmask_irq(n)	(REG_INTC_ICMCR((n)/32) = (1 << ((n)%32)))
#define __intc_mask_irq(n)	(REG_INTC_ICMSR((n)/32) = (1 << ((n)%32)))

#endif /* __MIPS_ASSEMBLER */

/*
 * AC97 and I2S controller module(AIC) address definition
 */
#define	AIC_BASE	0xb0020000

/*
 * AIC registers offset address definition
 */
#define AIC_FR_OFFSET		(0x00)
#define AIC_CR_OFFSET		(0x04)
#define AIC_ACCR1_OFFSET	(0x08)
#define AIC_ACCR2_OFFSET	(0x0c)
#define AIC_I2SCR_OFFSET	(0x10)
#define AIC_SR_OFFSET		(0x14)
#define AIC_ACSR_OFFSET		(0x18)
#define AIC_I2SSR_OFFSET	(0x1c)
#define AIC_ACCAR_OFFSET	(0x20)
#define AIC_ACCDR_OFFSET	(0x24)
#define AIC_ACSAR_OFFSET	(0x28)
#define AIC_ACSDR_OFFSET	(0x2c)
#define AIC_I2SDIV_OFFSET	(0x30)
#define AIC_DR_OFFSET		(0x34)

#define SPDIF_ENA_OFFSET	(0x80)
#define SPDIF_CTRL_OFFSET	(0x84)
#define SPDIF_STATE_OFFSET	(0x88)
#define SPDIF_CFG1_OFFSET	(0x8c)
#define SPDIF_CFG2_OFFSET	(0x90)
#define SPDIF_FIFO_OFFSET	(0x94)

#define ICDC_RGADW_OFFSET	(0xa4)
#define ICDC_RGDATA_OFFSET	(0xa8)

/*
 * AIC registers address definition
 */
#define AIC_FR		(AIC_BASE + AIC_FR_OFFSET)
#define AIC_CR		(AIC_BASE + AIC_CR_OFFSET)
#define AIC_ACCR1	(AIC_BASE + AIC_ACCR1_OFFSET)
#define AIC_ACCR2	(AIC_BASE + AIC_ACCR2_OFFSET)
#define AIC_I2SCR	(AIC_BASE + AIC_I2SCR_OFFSET)
#define AIC_SR		(AIC_BASE + AIC_SR_OFFSET)
#define AIC_ACSR	(AIC_BASE + AIC_ACSR_OFFSET)
#define AIC_I2SSR	(AIC_BASE + AIC_I2SSR_OFFSET)
#define AIC_ACCAR	(AIC_BASE + AIC_ACCAR_OFFSET)
#define AIC_ACCDR	(AIC_BASE + AIC_ACCDR_OFFSET)
#define AIC_ACSAR	(AIC_BASE + AIC_ACSAR_OFFSET)
#define AIC_ACSDR	(AIC_BASE + AIC_ACSDR_OFFSET)
#define AIC_I2SDIV	(AIC_BASE + AIC_I2SDIV_OFFSET)
#define AIC_DR		(AIC_BASE + AIC_DR_OFFSET)

#define SPDIF_ENA	(AIC_BASE + SPDIF_ENA_OFFSET)
#define SPDIF_CTRL	(AIC_BASE + SPDIF_CTRL_OFFSET)
#define SPDIF_STATE	(AIC_BASE + SPDIF_STATE_OFFSET)
#define SPDIF_CFG1	(AIC_BASE + SPDIF_CFG1_OFFSET)
#define SPDIF_CFG2	(AIC_BASE + SPDIF_CFG2_OFFSET)
#define SPDIF_FIFO	(AIC_BASE + SPDIF_FIFO_OFFSET)

#define ICDC_RGADW	(AIC_BASE + ICDC_RGADW_OFFSET)
#define ICDC_RGDATA	(AIC_BASE + ICDC_RGDATA_OFFSET)

/*
 * AIC registers common define
 */

/* AIC controller configuration register(AICFR) */
#define	AIC_FR_LSMP		BIT6
#define	AIC_FR_ICDC		BIT5
#define	AIC_FR_AUSEL		BIT4
#define	AIC_FR_RST		BIT3
#define	AIC_FR_BCKD		BIT2
#define	AIC_FR_SYNCD		BIT1
#define	AIC_FR_ENB		BIT0

#define	AIC_FR_RFTH_LSB		24
#define	AIC_FR_RFTH_MASK	BITS_H2L(27, AIC_FR_RFTH_LSB)

#define	AIC_FR_TFTH_LSB		16
#define	AIC_FR_TFTH_MASK	BITS_H2L(20, AIC_FR_TFTH_LSB)

/* AIC controller common control register(AICCR) */
#define AIC_CR_PACK16		BIT28
#define	AIC_CR_RDMS		BIT15
#define	AIC_CR_TDMS		BIT14
#define	AIC_CR_M2S		BIT11
#define	AIC_CR_ENDSW		BIT10
#define	AIC_CR_AVSTSU		BIT9
#define	AIC_CR_TFLUSH		BIT8
#define	AIC_CR_RFLUSH		BIT7
#define	AIC_CR_EROR		BIT6
#define	AIC_CR_ETUR		BIT5
#define	AIC_CR_ERFS		BIT4
#define	AIC_CR_ETFS		BIT3
#define	AIC_CR_ENLBF		BIT2
#define	AIC_CR_ERPL		BIT1
#define	AIC_CR_EREC		BIT0

#define AIC_CR_CHANNEL_LSB	24
#define AIC_CR_CHANNEL_MASK	BITS_H2L(26, AIC_CR_CHANNEL_LSB)

#define	AIC_CR_OSS_LSB		19
#define	AIC_CR_OSS_MASK		BITS_H2L(21, AIC_CR_OSS_LSB)
 #define AIC_CR_OSS(n)		(((n) > 18 ? (n)/6 : (n)/9) << AIC_CR_OSS_LSB)	/* n = 8, 16, 18, 20, 24 */

#define	AIC_CR_ISS_LSB		16
#define	AIC_CR_ISS_MASK		BITS_H2L(18, AIC_CR_ISS_LSB)
 #define AIC_CR_ISS(n)		(((n) > 18 ? (n)/6 : (n)/9) << AIC_CR_ISS_LSB)	/* n = 8, 16, 18, 20, 24 */

/* AIC controller AC-link control register 1(ACCR1) */
#define AIC_ACCR1_RS_LSB	16
#define	AIC_ACCR1_RS_MASK	BITS_H2L(25, AIC_ACCR1_RS_LSB)
 #define AIC_ACCR1_RS_SLOT(n)	((1 << ((n) - 3)) << AIC_ACCR1_RS_LSB)	/* n = 3 .. 12 */

#define AIC_ACCR1_XS_LSB	0
#define	AIC_ACCR1_XS_MASK	BITS_H2L(9, AIC_ACCR1_XS_LSB)
 #define AIC_ACCR1_XS_SLOT(n)	((1 << ((n) - 3)) << AIC_ACCR1_XS_LSB)	/* n = 3 .. 12 */

/* AIC controller AC-link control register 2 (ACCR2) */
#define	AIC_ACCR2_ERSTO		BIT18
#define	AIC_ACCR2_ESADR		BIT17
#define	AIC_ACCR2_ECADT		BIT16
#define	AIC_ACCR2_SO		BIT3
#define	AIC_ACCR2_SR		BIT2
#define	AIC_ACCR2_SS		BIT1
#define	AIC_ACCR2_SA		BIT0

/* AIC controller i2s/msb-justified control register (I2SCR) */
#define	AIC_I2SCR_RFIRST	BIT17
#define	AIC_I2SCR_SWLH   	BIT16
#define	AIC_I2SCR_STPBK		BIT12
#define AIC_I2SCR_ESCLK		BIT4
#define	AIC_I2SCR_AMSL		BIT0

/* AIC controller FIFO status register (AICSR) */
#define	AIC_SR_ROR		BIT6
#define	AIC_SR_TUR		BIT5
#define	AIC_SR_RFS		BIT4
#define	AIC_SR_TFS		BIT3

#define	AIC_SR_RFL_LSB		24
#define	AIC_SR_RFL_MASK		BITS_H2L(29, AIC_SR_RFL_LSB)

#define	AIC_SR_TFL_LSB		8
#define	AIC_SR_TFL_MASK		BITS_H2L(13, AIC_SR_TFL_LSB)

/* AIC controller AC-link status register (ACSR) */
#define	AIC_ACSR_SLTERR		BIT21
#define	AIC_ACSR_CRDY		BIT20
#define	AIC_ACSR_CLPM		BIT19
#define	AIC_ACSR_RSTO		BIT18
#define	AIC_ACSR_SADR		BIT17
#define	AIC_ACSR_CADT		BIT16

/* AIC controller I2S/MSB-justified status register (I2SSR) */
#define	AIC_I2SSR_CHBSY		BIT5
#define	AIC_I2SSR_TBSY		BIT4
#define	AIC_I2SSR_RBSY		BIT3
#define	AIC_I2SSR_BSY		BIT2

/* AIC controller AC97 codec command address register (ACCAR) */
#define	AIC_ACCAR_CAR_LSB	0
#define	AIC_ACCAR_CAR_MASK	BITS_H2L(19, AIC_ACCAR_CAR_LSB)

/* AIC controller AC97 codec command data register (ACCDR) */
#define	AIC_ACCDR_CDR_LSB	0
#define	AIC_ACCDR_CDR_MASK	BITS_H2L(19, AIC_ACCDR_CDR_LSB)

/* AC97 read and write macro based on ACCAR and ACCDR */
#define AC97_READ_CMD		BIT19
#define AC97_WRITE_CMD		(BIT19 & ~BIT19)

#define AC97_INDEX_LSB		12
#define AC97_INDEX_MASK		BITS_H2L(18, AC97_INDEX_LSB)

#define AC97_DATA_LSB		4
#define AC97_DATA_MASK		BITS_H2L(19, AC97_DATA_LSB)

/* AIC controller AC97 codec status address register (ACSAR) */
#define	AIC_ACSAR_SAR_LSB	0
#define	AIC_ACSAR_SAR_MASK	BITS_H2L(19, AIC_ACSAR_SAR_LSB)

/* AIC controller AC97 codec status data register (ACSDR) */
#define	AIC_ACSDR_SDR_LSB	0
#define	AIC_ACSDR_SDR_MASK	BITS_H2L(19, AIC_ACSDR_SDR_LSB)

/* AIC controller I2S/MSB-justified clock divider register (I2SDIV) */
#define	AIC_I2SDIV_DIV_LSB	0
#define	AIC_I2SDIV_DIV_MASK	BITS_H2L(3, AIC_I2SDIV_DIV_LSB)

/* SPDIF enable register (SPDIF_ENA) */
#define SPDIF_ENA_SPEN		BIT0

/* SPDIF control register (SPDIF_CTRL) */
#define SPDIF_CTRL_DMAEN        BIT15
#define SPDIF_CTRL_DTYPE        BIT14
#define SPDIF_CTRL_SIGN         BIT13
#define SPDIF_CTRL_INVALID      BIT12
#define SPDIF_CTRL_RST          BIT11
#define SPDIF_CTRL_SPDIFI2S     BIT10
#define SPDIF_CTRL_MTRIG        BIT1
#define SPDIF_CTRL_MFFUR        BIT0

/* SPDIF state register (SPDIF_STAT) */
#define SPDIF_STAT_BUSY		BIT7
#define SPDIF_STAT_FTRIG	BIT1
#define SPDIF_STAT_FUR		BIT0

#define SPDIF_STAT_FLVL_LSB	8
#define SPDIF_STAT_FLVL_MASK	BITS_H2L(14, SPDIF_STAT_FLVL_LSB)

/* SPDIF configure 1 register (SPDIF_CFG1) */
#define SPDIF_CFG1_INITLVL	BIT17
#define SPDIF_CFG1_ZROVLD	BIT16

#define SPDIF_CFG1_TRIG_LSB	12
#define SPDIF_CFG1_TRIG_MASK	BITS_H2L(13, SPDIF_CFG1_TRIG_LSB)
#define SPDIF_CFG1_TRIG(n)	(((n) > 16 ? 3 : (n)/8) << SPDIF_CFG1_TRIG_LSB)	/* n = 4, 8, 16, 32 */

#define SPDIF_CFG1_SRCNUM_LSB	8
#define SPDIF_CFG1_SRCNUM_MASK	BITS_H2L(11, SPDIF_CFG1_SRCNUM_LSB)

#define SPDIF_CFG1_CH1NUM_LSB	4
#define SPDIF_CFG1_CH1NUM_MASK	BITS_H2L(7, SPDIF_CFG1_CH1NUM_LSB)

#define SPDIF_CFG1_CH2NUM_LSB	0
#define SPDIF_CFG1_CH2NUM_MASK	BITS_H2L(3, SPDIF_CFG1_CH2NUM_LSB)

/* SPDIF configure 2 register (SPDIF_CFG2) */
#define SPDIF_CFG2_MAXWL	BIT18
#define SPDIF_CFG2_PRE		BIT3
#define SPDIF_CFG2_COPYN	BIT2
#define SPDIF_CFG2_AUDION	BIT1
#define SPDIF_CFG2_CONPRO	BIT0

#define SPDIF_CFG2_FS_LSB	26
#define SPDIF_CFG2_FS_MASK	BITS_H2L(29, SPDIF_CFG2_FS_LSB)

#define SPDIF_CFG2_ORGFRQ_LSB	22
#define SPDIF_CFG2_ORGFRQ_MASK	BITS_H2L(25, SPDIF_CFG2_ORGFRQ_LSB)

#define SPDIF_CFG2_SAMWL_LSB	19
#define SPDIF_CFG2_SAMWL_MASK	BITS_H2L(21, SPDIF_CFG2_SAMWL_LSB)

#define SPDIF_CFG2_CLKACU_LSB	16
#define SPDIF_CFG2_CLKACU_MASK	BITS_H2L(17, SPDIF_CFG2_CLKACU_LSB)

#define SPDIF_CFG2_CATCODE_LSB	8
#define SPDIF_CFG2_CATCODE_MASK	BITS_H2L(15, SPDIF_CFG2_CATCODE_LSB)

#define SPDIF_CFG2_CHMD_LSB	6
#define SPDIF_CFG2_CHMD_MASK	BITS_H2L(7, SPDIF_CFG2_CHMD_LSB)

/* ICDC internal register access control register(RGADW) */
#define ICDC_RGADW_RGWR		BIT16

#define ICDC_RGADW_RGADDR_LSB	8
#define	ICDC_RGADW_RGADDR_MASK	BITS_H2L(14, ICDC_RGADW_RGADDR_LSB)

#define ICDC_RGADW_RGDIN_LSB	0
#define	ICDC_RGADW_RGDIN_MASK	BITS_H2L(7, ICDC_RGADW_RGDIN_LSB)

/* ICDC internal register data output register (RGDATA)*/
#define ICDC_RGDATA_IRQ		BIT8

#define ICDC_RGDATA_RGDOUT_LSB	0
#define ICDC_RGDATA_RGDOUT_MASK	BITS_H2L(7, ICDC_RGDATA_RGDOUT_LSB)

#ifndef __MIPS_ASSEMBLER

#define	REG_AIC_FR		REG32(AIC_FR)
#define	REG_AIC_CR		REG32(AIC_CR)
#define	REG_AIC_ACCR1		REG32(AIC_ACCR1)
#define	REG_AIC_ACCR2		REG32(AIC_ACCR2)
#define	REG_AIC_I2SCR		REG32(AIC_I2SCR)
#define	REG_AIC_SR		REG32(AIC_SR)
#define	REG_AIC_ACSR		REG32(AIC_ACSR)
#define	REG_AIC_I2SSR		REG32(AIC_I2SSR)
#define	REG_AIC_ACCAR		REG32(AIC_ACCAR)
#define	REG_AIC_ACCDR		REG32(AIC_ACCDR)
#define	REG_AIC_ACSAR		REG32(AIC_ACSAR)
#define	REG_AIC_ACSDR		REG32(AIC_ACSDR)
#define	REG_AIC_I2SDIV		REG32(AIC_I2SDIV)
#define	REG_AIC_DR		REG32(AIC_DR)

#define REG_SPDIF_ENA		REG32(SPDIF_ENA)
#define REG_SPDIF_CTRL		REG32(SPDIF_CTRL)
#define REG_SPDIF_STATE		REG32(SPDIF_STATE)
#define REG_SPDIF_CFG1		REG32(SPDIF_CFG1)
#define REG_SPDIF_CFG2		REG32(SPDIF_CFG2)
#define REG_SPDIF_FIFO		REG32(SPDIF_FIFO)

#define	REG_ICDC_RGADW		REG32(ICDC_RGADW)
#define	REG_ICDC_RGDATA		REG32(ICDC_RGDATA)

#define __aic_enable()		( REG_AIC_FR |= AIC_FR_ENB )
#define __aic_disable()		( REG_AIC_FR &= ~AIC_FR_ENB )

#define __aic_select_ac97()	( REG_AIC_FR &= ~AIC_FR_AUSEL )
#define __aic_select_i2s()	( REG_AIC_FR |= AIC_FR_AUSEL )

#define __aic_play_zero()	( REG_AIC_FR &= ~AIC_FR_LSMP )
#define __aic_play_lastsample()	( REG_AIC_FR |= AIC_FR_LSMP )

#define __i2s_as_master()	( REG_AIC_FR |= AIC_FR_BCKD | AIC_FR_SYNCD )
#define __i2s_as_slave()	( REG_AIC_FR &= ~(AIC_FR_BCKD | AIC_FR_SYNCD) )
#define __aic_reset_status()          ( REG_AIC_FR & AIC_FR_RST )

#define __i2s_enable_sclk()	( REG_AIC_I2SCR |= AIC_I2SCR_ESCLK )
#define __i2s_disable_sclk()	( REG_AIC_I2SCR &= ~AIC_I2SCR_ESCLK )

#define __aic_reset()                                   \
do {                                                    \
        REG_AIC_FR |= AIC_FR_RST;                       \
} while(0)

#define __aic_set_transmit_trigger(n) 			\
do {							\
	REG_AIC_FR &= ~AIC_FR_TFTH_MASK;		\
	REG_AIC_FR |= ((n) << AIC_FR_TFTH_LSB);		\
} while(0)

#define __aic_set_receive_trigger(n) 			\
do {							\
	REG_AIC_FR &= ~AIC_FR_RFTH_MASK;		\
	REG_AIC_FR |= ((n) << AIC_FR_RFTH_LSB);		\
} while(0)

#define __aic_enable_oldstyle()
#define __aic_enable_newstyle()
#define __aic_enable_pack16()   ( REG_AIC_CR |= AIC_CR_PACK16 )
#define __aic_enable_unpack16() ( REG_AIC_CR &= ~AIC_CR_PACK16)

/* n = AIC_CR_CHANNEL_MONO,AIC_CR_CHANNEL_STEREO ... */
#define __aic_out_channel_select(n)                    \
do {                                                   \
	REG_AIC_CR &= ~AIC_CR_CHANNEL_MASK;            \
        REG_AIC_CR |= ((n) << AIC_CR_CHANNEL_LSB );			       \
} while(0)

#define __aic_enable_record()	( REG_AIC_CR |= AIC_CR_EREC )
#define __aic_disable_record()	( REG_AIC_CR &= ~AIC_CR_EREC )
#define __aic_enable_replay()	( REG_AIC_CR |= AIC_CR_ERPL )
#define __aic_disable_replay()	( REG_AIC_CR &= ~AIC_CR_ERPL )
#define __aic_enable_loopback()	( REG_AIC_CR |= AIC_CR_ENLBF )
#define __aic_disable_loopback() ( REG_AIC_CR &= ~AIC_CR_ENLBF )

#define __aic_flush_tfifo()	( REG_AIC_CR |= AIC_CR_TFLUSH )
#define __aic_unflush_tfifo()	( REG_AIC_CR &= ~AIC_CR_TFLUSH )
#define __aic_flush_rfifo()	( REG_AIC_CR |= AIC_CR_RFLUSH )
#define __aic_unflush_rfifo()	( REG_AIC_CR &= ~AIC_CR_RFLUSH )

#define __aic_enable_transmit_intr() \
  ( REG_AIC_CR |= (AIC_CR_ETFS | AIC_CR_ETUR) )
#define __aic_disable_transmit_intr() \
  ( REG_AIC_CR &= ~(AIC_CR_ETFS | AIC_CR_ETUR) )
#define __aic_enable_receive_intr() \
  ( REG_AIC_CR |= (AIC_CR_ERFS | AIC_CR_EROR) )
#define __aic_disable_receive_intr() \
  ( REG_AIC_CR &= ~(AIC_CR_ERFS | AIC_CR_EROR) )

#define __aic_enable_transmit_dma()  ( REG_AIC_CR |= AIC_CR_TDMS )
#define __aic_disable_transmit_dma() ( REG_AIC_CR &= ~AIC_CR_TDMS )
#define __aic_enable_receive_dma()   ( REG_AIC_CR |= AIC_CR_RDMS )
#define __aic_disable_receive_dma()  ( REG_AIC_CR &= ~AIC_CR_RDMS )

#define __aic_enable_mono2stereo()   ( REG_AIC_CR |= AIC_CR_M2S )
#define __aic_disable_mono2stereo()  ( REG_AIC_CR &= ~AIC_CR_M2S )
#define __aic_enable_byteswap()      ( REG_AIC_CR |= AIC_CR_ENDSW )
#define __aic_disable_byteswap()     ( REG_AIC_CR &= ~AIC_CR_ENDSW )
#define __aic_enable_unsignadj()     ( REG_AIC_CR |= AIC_CR_AVSTSU )
#define __aic_disable_unsignadj()    ( REG_AIC_CR &= ~AIC_CR_AVSTSU )

#define AC97_PCM_XS_L_FRONT   	AIC_ACCR1_XS_SLOT(3)
#define AC97_PCM_XS_R_FRONT   	AIC_ACCR1_XS_SLOT(4)
#define AC97_PCM_XS_CENTER    	AIC_ACCR1_XS_SLOT(6)
#define AC97_PCM_XS_L_SURR    	AIC_ACCR1_XS_SLOT(7)
#define AC97_PCM_XS_R_SURR    	AIC_ACCR1_XS_SLOT(8)
#define AC97_PCM_XS_LFE       	AIC_ACCR1_XS_SLOT(9)

#define AC97_PCM_RS_L_FRONT   	AIC_ACCR1_RS_SLOT(3)
#define AC97_PCM_RS_R_FRONT   	AIC_ACCR1_RS_SLOT(4)
#define AC97_PCM_RS_CENTER    	AIC_ACCR1_RS_SLOT(6)
#define AC97_PCM_RS_L_SURR    	AIC_ACCR1_RS_SLOT(7)
#define AC97_PCM_RS_R_SURR    	AIC_ACCR1_RS_SLOT(8)
#define AC97_PCM_RS_LFE       	AIC_ACCR1_RS_SLOT(9)

#define __ac97_set_xs_none()	( REG_AIC_ACCR1 &= ~AIC_ACCR1_XS_MASK )
#define __ac97_set_xs_mono() 						\
do {									\
	REG_AIC_ACCR1 &= ~AIC_ACCR1_XS_MASK;				\
	REG_AIC_ACCR1 |= AC97_PCM_XS_R_FRONT;				\
} while(0)
#define __ac97_set_xs_stereo() 						\
do {									\
	REG_AIC_ACCR1 &= ~AIC_ACCR1_XS_MASK;				\
	REG_AIC_ACCR1 |= AC97_PCM_XS_L_FRONT | AC97_PCM_XS_R_FRONT;	\
} while(0)

/* In fact, only stereo is support now. */
#define __ac97_set_rs_none()	( REG_AIC_ACCR1 &= ~AIC_ACCR1_RS_MASK )
#define __ac97_set_rs_mono() 						\
do {									\
	REG_AIC_ACCR1 &= ~AIC_ACCR1_RS_MASK;				\
	REG_AIC_ACCR1 |= AC97_PCM_RS_R_FRONT;				\
} while(0)
#define __ac97_set_rs_stereo() 						\
do {									\
	REG_AIC_ACCR1 &= ~AIC_ACCR1_RS_MASK;				\
	REG_AIC_ACCR1 |= AC97_PCM_RS_L_FRONT | AC97_PCM_RS_R_FRONT;	\
} while(0)

#define __ac97_warm_reset_codec()		\
 do {						\
	REG_AIC_ACCR2 |= AIC_ACCR2_SA;		\
	REG_AIC_ACCR2 |= AIC_ACCR2_SS;		\
	udelay(2);				\
	REG_AIC_ACCR2 &= ~AIC_ACCR2_SS;		\
	REG_AIC_ACCR2 &= ~AIC_ACCR2_SA;		\
 } while (0)

#define __ac97_cold_reset_codec()		\
 do {						\
	REG_AIC_ACCR2 |=  AIC_ACCR2_SR;		\
	udelay(2);				\
	REG_AIC_ACCR2 &= ~AIC_ACCR2_SR;		\
 } while (0)

/* n=8,16,18,20 */
#define __ac97_set_iass(n) \
 ( REG_AIC_ACCR2 = (REG_AIC_ACCR2 & ~AIC_ACCR2_IASS_MASK) | AIC_ACCR2_IASS_##n##BIT )
#define __ac97_set_oass(n) \
 ( REG_AIC_ACCR2 = (REG_AIC_ACCR2 & ~AIC_ACCR2_OASS_MASK) | AIC_ACCR2_OASS_##n##BIT )

/* This bit should only be set in 2 channels configuration */
#define __i2s_send_rfirst()   ( REG_AIC_I2SCR |= AIC_I2SCR_RFIRST )  /* RL */
#define __i2s_send_lfirst()   ( REG_AIC_I2SCR &= ~AIC_I2SCR_RFIRST ) /* LR */

/* This bit should only be set in 2 channels configuration and 16bit-packed mode */
#define __i2s_switch_lr()     ( REG_AIC_I2SCR |= AIC_I2SCR_SWLH )
#define __i2s_unswitch_lr()   ( REG_AIC_I2SCR &= ~AIC_I2SCR_SWLH )

#define __i2s_select_i2s()            ( REG_AIC_I2SCR &= ~AIC_I2SCR_AMSL )
#define __i2s_select_msbjustified()   ( REG_AIC_I2SCR |= AIC_I2SCR_AMSL )

/* n=8,16,18,20,24 */
/*#define __i2s_set_sample_size(n) \
 ( REG_AIC_I2SCR |= (REG_AIC_I2SCR & ~AIC_I2SCR_WL_MASK) | AIC_I2SCR_WL_##n##BIT )*/

#define __i2s_out_channel_select(n) __aic_out_channel_select(n)

#define __i2s_set_oss_sample_size(n) \
 ( REG_AIC_CR = (REG_AIC_CR & ~AIC_CR_OSS_MASK) | AIC_CR_OSS(n))
#define __i2s_set_iss_sample_size(n) \
 ( REG_AIC_CR = (REG_AIC_CR & ~AIC_CR_ISS_MASK) | AIC_CR_ISS(n))

#define __i2s_stop_bitclk()   ( REG_AIC_I2SCR |= AIC_I2SCR_STPBK )
#define __i2s_start_bitclk()  ( REG_AIC_I2SCR &= ~AIC_I2SCR_STPBK )

#define __aic_transmit_request()  ( REG_AIC_SR & AIC_SR_TFS )
#define __aic_receive_request()   ( REG_AIC_SR & AIC_SR_RFS )
#define __aic_transmit_underrun() ( REG_AIC_SR & AIC_SR_TUR )
#define __aic_receive_overrun()   ( REG_AIC_SR & AIC_SR_ROR )

#define __aic_clear_errors()      ( REG_AIC_SR &= ~(AIC_SR_TUR | AIC_SR_ROR) )

#define __aic_get_transmit_resident() \
  ( (REG_AIC_SR & AIC_SR_TFL_MASK) >> AIC_SR_TFL_LSB )
#define __aic_get_receive_count() \
  ( (REG_AIC_SR & AIC_SR_RFL_MASK) >> AIC_SR_RFL_LSB )

#define __ac97_command_transmitted()     ( REG_AIC_ACSR & AIC_ACSR_CADT )
#define __ac97_status_received()         ( REG_AIC_ACSR & AIC_ACSR_SADR )
#define __ac97_status_receive_timeout()  ( REG_AIC_ACSR & AIC_ACSR_RSTO )
#define __ac97_codec_is_low_power_mode() ( REG_AIC_ACSR & AIC_ACSR_CLPM )
#define __ac97_codec_is_ready()          ( REG_AIC_ACSR & AIC_ACSR_CRDY )
#define __ac97_slot_error_detected()     ( REG_AIC_ACSR & AIC_ACSR_SLTERR )
#define __ac97_clear_slot_error()        ( REG_AIC_ACSR &= ~AIC_ACSR_SLTERR )

#define __i2s_is_busy()         ( REG_AIC_I2SSR & AIC_I2SSR_BSY )

#define __ac97_out_rcmd_addr(reg) 					\
do { 									\
    REG_AIC_ACCAR = AC97_READ_CMD | ((reg) << AC97_INDEX_LSB); 	\
} while (0)

#define __ac97_out_wcmd_addr(reg) 					\
do { 									\
    REG_AIC_ACCAR = AC97_WRITE_CMD | ((reg) << AC97_INDEX_LSB); 	\
} while (0)

#define __ac97_out_data(value) 						\
do { 									\
    REG_AIC_ACCDR = ((value) << AC97_DATA_LSB); 			\
} while (0)

#define __ac97_in_data() \
 ( (REG_AIC_ACSDR & CODEC_REG_DATA_MASK) >> AC97_DATA_LSB )

#define __ac97_in_status_addr() \
 ( (REG_AIC_ACSAR & AC97_INDEX_MASK) >> AC97_INDEX_LSB )

#define __i2s_set_sample_rate(i2sclk, sync) \
  ( REG_AIC_I2SDIV = ((i2sclk) / (4*64)) / (sync) )

#define __i2s_set_i2sdiv(n) \
  ( REG_AIC_I2SDIV = (n) )

#define __aic_write_tfifo(v)  ( REG_AIC_DR = (v) )
#define __aic_read_rfifo()    ( REG_AIC_DR )

#define __aic_internal_codec()  ( REG_AIC_FR |= AIC_FR_ICDC )
#define __aic_external_codec()  ( REG_AIC_FR &= ~AIC_FR_ICDC )

//
// Define next ops for AC97 compatible
//

#define AC97_ACSR	AIC_ACSR

#define __ac97_enable()		__aic_enable(); __aic_select_ac97()
#define __ac97_disable()	__aic_disable()
#define __ac97_reset()		__aic_reset()

#define __ac97_set_transmit_trigger(n)	__aic_set_transmit_trigger(n)
#define __ac97_set_receive_trigger(n)	__aic_set_receive_trigger(n)

#define __ac97_enable_record()		__aic_enable_record()
#define __ac97_disable_record()		__aic_disable_record()
#define __ac97_enable_replay()		__aic_enable_replay()
#define __ac97_disable_replay()		__aic_disable_replay()
#define __ac97_enable_loopback()	__aic_enable_loopback()
#define __ac97_disable_loopback()	__aic_disable_loopback()

#define __ac97_enable_transmit_dma()	__aic_enable_transmit_dma()
#define __ac97_disable_transmit_dma()	__aic_disable_transmit_dma()
#define __ac97_enable_receive_dma()	__aic_enable_receive_dma()
#define __ac97_disable_receive_dma()	__aic_disable_receive_dma()

#define __ac97_transmit_request()	__aic_transmit_request()
#define __ac97_receive_request()	__aic_receive_request()
#define __ac97_transmit_underrun()	__aic_transmit_underrun()
#define __ac97_receive_overrun()	__aic_receive_overrun()

#define __ac97_clear_errors()		__aic_clear_errors()

#define __ac97_get_transmit_resident()	__aic_get_transmit_resident()
#define __ac97_get_receive_count()	__aic_get_receive_count()

#define __ac97_enable_transmit_intr()	__aic_enable_transmit_intr()
#define __ac97_disable_transmit_intr()	__aic_disable_transmit_intr()
#define __ac97_enable_receive_intr()	__aic_enable_receive_intr()
#define __ac97_disable_receive_intr()	__aic_disable_receive_intr()

#define __ac97_write_tfifo(v)		__aic_write_tfifo(v)
#define __ac97_read_rfifo()		__aic_read_rfifo()

//
// Define next ops for I2S compatible
//

#define I2S_ACSR	AIC_I2SSR

#define __i2s_enable()		 __aic_enable(); __aic_select_i2s()
#define __i2s_disable()		__aic_disable()
#define __i2s_reset()		__aic_reset()

#define __i2s_set_transmit_trigger(n)	__aic_set_transmit_trigger(n)
#define __i2s_set_receive_trigger(n)	__aic_set_receive_trigger(n)

#define __i2s_enable_record()		__aic_enable_record()
#define __i2s_disable_record()		__aic_disable_record()
#define __i2s_enable_replay()		__aic_enable_replay()
#define __i2s_disable_replay()		__aic_disable_replay()
#define __i2s_enable_loopback()		__aic_enable_loopback()
#define __i2s_disable_loopback()	__aic_disable_loopback()

#define __i2s_enable_transmit_dma()	__aic_enable_transmit_dma()
#define __i2s_disable_transmit_dma()	__aic_disable_transmit_dma()
#define __i2s_enable_receive_dma()	__aic_enable_receive_dma()
#define __i2s_disable_receive_dma()	__aic_disable_receive_dma()

#define __i2s_transmit_request()	__aic_transmit_request()
#define __i2s_receive_request()		__aic_receive_request()
#define __i2s_transmit_underrun()	__aic_transmit_underrun()
#define __i2s_receive_overrun()		__aic_receive_overrun()

#define __i2s_clear_errors()		__aic_clear_errors()

#define __i2s_get_transmit_resident()	__aic_get_transmit_resident()
#define __i2s_get_receive_count()	__aic_get_receive_count()

#define __i2s_enable_transmit_intr()	__aic_enable_transmit_intr()
#define __i2s_disable_transmit_intr()	__aic_disable_transmit_intr()
#define __i2s_enable_receive_intr()	__aic_enable_receive_intr()
#define __i2s_disable_receive_intr()	__aic_disable_receive_intr()

#define __i2s_write_tfifo(v)		__aic_write_tfifo(v)
#define __i2s_read_rfifo()		__aic_read_rfifo()

#define __i2s_enable_pack16()		__aic_enable_pack16()
#define __i2s_enable_unpack16()		__aic_enable_unpack16()

#define __i2s_reset_codec()			\
 do {						\
 } while (0)

/*************************************************************************
 * SPDIF INTERFACE in AIC Controller
 *************************************************************************/

#define __spdif_enable()        ( REG_SPDIF_ENA |= SPDIF_ENA_SPEN )
#define __spdif_disable()       ( REG_SPDIF_ENA &= ~SPDIF_ENA_SPEN )

#define __spdif_enable_transmit_dma()     ( REG_SPDIF_CTRL |= SPDIF_CTRL_DMAEN )
#define __spdif_disable_transmit_dma()    ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_DMAEN )
#define __spdif_enable_dtype()            ( REG_SPDIF_CTRL |= SPDIF_CTRL_DTYPE )
#define __spdif_disable_dtype()           ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_DTYPE )
#define __spdif_enable_sign()             ( REG_SPDIF_CTRL |= SPDIF_CTRL_SIGN )
#define __spdif_disable_sign()            ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_SIGN )
#define __spdif_enable_invalid()          ( REG_SPDIF_CTRL |= SPDIF_CTRL_INVALID )
#define __spdif_disable_invalid()         ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_INVALID )
#define __spdif_enable_reset()            ( REG_SPDIF_CTRL |= SPDIF_CTRL_RST )
#define __spdif_select_spdif()            ( REG_SPDIF_CTRL |= SPDIF_CTRL_SPDIFI2S )
#define __spdif_select_i2s()              ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_SPDIFI2S )
#define __spdif_enable_MTRIGmask()        ( REG_SPDIF_CTRL |= SPDIF_CTRL_MTRIG )
#define __spdif_disable_MTRIGmask()       ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_MTRIG )
#define __spdif_enable_MFFURmask()        ( REG_SPDIF_CTRL |= SPDIF_CTRL_MFFUR )
#define __spdif_disable_MFFURmask()       ( REG_SPDIF_CTRL &= ~SPDIF_CTRL_MFFUR )

#define __spdif_enable_initlvl_high()     ( REG_SPDIF_CFG1 |=  SPDIF_CFG1_INITLVL )
#define __spdif_enable_initlvl_low()      ( REG_SPDIF_CFG1 &=  ~SPDIF_CFG1_INITLVL )
#define __spdif_enable_zrovld_invald()    ( REG_SPDIF_CFG1 |=  SPDIF_CFG1_ZROVLD )
#define __spdif_enable_zrovld_vald()      ( REG_SPDIF_CFG1 &=  ~SPDIF_CFG1_ZROVLD )

/* 0, 1, 2, 3 */
#define __spdif_set_transmit_trigger(n)			\
do {							\
	REG_SPDIF_CFG1 &= ~SPDIF_CFG1_TRIG_MASK;	\
	REG_SPDIF_CFG1 |= SPDIF_CFG1_TRIG(n);	\
} while(0)

/* 1 ~ 15 */
#define __spdif_set_srcnum(n)    			\
do {							\
	REG_SPDIF_CFG1 &= ~SPDIF_CFG1_SRCNUM_MASK;	\
	REG_SPDIF_CFG1 |= ((n) << SPDIF_CFG1_SRCNUM_LSB);	\
} while(0)

/* 1 ~ 15 */
#define __spdif_set_ch1num(n)    			\
do {							\
	REG_SPDIF_CFG1 &= ~SPDIF_CFG1_CH1NUM_MASK;	\
	REG_SPDIF_CFG1 |= ((n) << SPDIF_CFG1_CH1NUM_LSB);	\
} while(0)

/* 1 ~ 15 */
#define __spdif_set_ch2num(n)    			\
do {							\
	REG_SPDIF_CFG1 &= ~SPDIF_CFG1_CH2NUM_MASK;	\
	REG_SPDIF_CFG1 |= ((n) << SPDIF_CFG1_CH2NUM_LSB);	\
} while(0)

/* 0x0, 0x2, 0x3, 0xa, 0xe */
#define __spdif_set_fs(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_FS_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_FS_LSB);	\
} while(0)

/* 0xd, 0xc, 0x5, 0x1 */
#define __spdif_set_orgfrq(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_ORGFRQ_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_ORGFRQ_LSB);	\
} while(0)

/* 0x1, 0x6, 0x2, 0x4, 0x5 */
#define __spdif_set_samwl(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_SAMWL_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_SAMWL_LSB);	\
} while(0)

#define __spdif_enable_samwl_24()    ( REG_SPDIF_CFG2 |= SPDIF_CFG2_MAXWL )
#define __spdif_enable_samwl_20()    ( REG_SPDIF_CFG1 &= ~SPDIF_CFG2_MAXWL )

/* 0x1, 0x1, 0x2, 0x3 */
#define __spdif_set_clkacu(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_CLKACU_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_CLKACU_LSB);	\
} while(0)

/* see IEC60958-3 */
#define __spdif_set_catcode(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_CATCODE_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_CATCODE_LSB);	\
} while(0)

/* n = 0x0, */
#define __spdif_set_chmode(n)				\
do {							\
	REG_SPDIF_CFG2 &= ~SPDIF_CFG2_CHMD_MASK;   	\
	REG_SPDIF_CFG2 |= ((n) << SPDIF_CFG2_CHMD_LSB);	\
} while(0)

#define __spdif_enable_pre()       ( REG_SPDIF_CFG2 |= SPDIF_CFG2_PRE )
#define __spdif_disable_pre()      ( REG_SPDIF_CFG2 &= ~SPDIF_CFG2_PRE )
#define __spdif_enable_copyn()     ( REG_SPDIF_CFG2 |= SPDIF_CFG2_COPYN )
#define __spdif_disable_copyn()    ( REG_SPDIF_CFG2 &= ~SPDIF_CFG2_COPYN )
/* audio sample word represents linear PCM samples */
#define __spdif_enable_audion()    ( REG_SPDIF_CFG2 &= ~SPDIF_CFG2_AUDION )
/* udio sample word used for other purpose */
#define __spdif_disable_audion()   ( REG_SPDIF_CFG2 |= SPDIF_CFG2_AUDION )
#define __spdif_enable_conpro()    ( REG_SPDIF_CFG2 &= ~SPDIF_CFG2_CONPRO )
#define __spdif_disable_conpro()   ( REG_SPDIF_CFG2 |= SPDIF_CFG2_CONPRO )

/***************************************************************************
 * ICDC
 ***************************************************************************/
#define __i2s_internal_codec()         __aic_internal_codec()
#define __i2s_external_codec()         __aic_external_codec()

#define __icdc_clk_ready()             ( REG_ICDC_CKCFG & ICDC_CKCFG_CKRDY )
#define __icdc_sel_adc()               ( REG_ICDC_CKCFG |= ICDC_CKCFG_SELAD )
#define __icdc_sel_dac()               ( REG_ICDC_CKCFG &= ~ICDC_CKCFG_SELAD )

#define __icdc_set_rgwr()              ( REG_ICDC_RGADW |= ICDC_RGADW_RGWR )
#define __icdc_clear_rgwr()            ( REG_ICDC_RGADW &= ~ICDC_RGADW_RGWR )
#define __icdc_rgwr_ready()            ( REG_ICDC_RGADW & ICDC_RGADW_RGWR )

#define __icdc_set_addr(n)				\
do {          						\
	REG_ICDC_RGADW &= ~ICDC_RGADW_RGADDR_MASK;	\
	REG_ICDC_RGADW |= (n) << ICDC_RGADW_RGADDR_LSB;	\
} while(0)

#define __icdc_set_cmd(n)				\
do {          						\
	REG_ICDC_RGADW &= ~ICDC_RGADW_RGDIN_MASK;	\
	REG_ICDC_RGADW |= (n) << ICDC_RGADW_RGDIN_LSB;	\
} while(0)

#define __icdc_irq_pending()            ( REG_ICDC_RGDATA & ICDC_RGDATA_IRQ )
#define __icdc_get_value()              ( REG_ICDC_RGDATA & ICDC_RGDATA_RGDOUT_MASK )

#endif /* __MIPS_ASSEMBLER */

/*
 * Bose-Chaudhuri-Hocquenghem controller module(BCH) address definition
 */
#define BCH_BASE	0xb34d0000

/*
 * BCH registers offset addresses definition
 */
#define	BCH_BHCR_OFFSET		(0x00)	/*  r, 32, 0x00000000 */
#define	BCH_BHCSR_OFFSET	(0x04)	/*  w, 32, 0x???????? */
#define	BCH_BHCCR_OFFSET	(0x08)	/*  w, 32, 0x???????? */
#define	BCH_BHCNT_OFFSET	(0x0c)	/* rw, 32, 0x00000000 */
#define	BCH_BHDR_OFFSET		(0x10)	/*  w,  8, 0x???????? */
#define	BCH_BHPAR0_OFFSET	(0x14)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR1_OFFSET	(0x18)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR2_OFFSET	(0x1c)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR3_OFFSET	(0x20)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR4_OFFSET	(0x24)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR5_OFFSET	(0x28)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR6_OFFSET	(0x2c)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR7_OFFSET	(0x30)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR8_OFFSET	(0x34)	/* rw, 32, 0x00000000 */
#define	BCH_BHPAR9_OFFSET	(0x38)	/* rw, 32, 0x00000000 */
#define	BCH_BHERR0_OFFSET	(0x3c)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR1_OFFSET	(0x40)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR2_OFFSET	(0x44)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR3_OFFSET	(0x48)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR4_OFFSET	(0x4c)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR5_OFFSET	(0x50)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR6_OFFSET	(0x54)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR7_OFFSET	(0x58)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR8_OFFSET	(0x5c)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR9_OFFSET	(0x60)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR10_OFFSET	(0x64)	/*  r, 32, 0x00000000 */
#define	BCH_BHERR11_OFFSET	(0x68)	/*  r, 32, 0x00000000 */
#define	BCH_BHINT_OFFSET	(0x6c)	/*  r, 32, 0x00000000 */
#define	BCH_BHINTE_OFFSET	(0x70)	/* rw, 32, 0x00000000 */
#define	BCH_BHINTES_OFFSET	(0x74)	/*  w, 32, 0x???????? */
#define	BCH_BHINTEC_OFFSET	(0x78)	/*  w, 32, 0x???????? */

/*
 * BCH registers addresses definition
 */
#define	BCH_BHCR        (BCH_BASE + BCH_BHCR_OFFSET)
#define	BCH_BHCSR       (BCH_BASE + BCH_BHCSR_OFFSET)
#define	BCH_BHCCR       (BCH_BASE + BCH_BHCCR_OFFSET)
#define	BCH_BHCNT    	(BCH_BASE + BCH_BHCNT_OFFSET)
#define	BCH_BHDR     	(BCH_BASE + BCH_BHDR_OFFSET)
#define	BCH_BHPAR0    	(BCH_BASE + BCH_BHPAR0_OFFSET)
#define	BCH_BHPAR1    	(BCH_BASE + BCH_BHPAR1_OFFSET)
#define	BCH_BHPAR2    	(BCH_BASE + BCH_BHPAR2_OFFSET)
#define	BCH_BHPAR3    	(BCH_BASE + BCH_BHPAR3_OFFSET)
#define	BCH_BHPAR4    	(BCH_BASE + BCH_BHPAR4_OFFSET)
#define	BCH_BHPAR5    	(BCH_BASE + BCH_BHPAR5_OFFSET)
#define	BCH_BHPAR6    	(BCH_BASE + BCH_BHPAR6_OFFSET)
#define	BCH_BHPAR7    	(BCH_BASE + BCH_BHPAR7_OFFSET)
#define	BCH_BHPAR8    	(BCH_BASE + BCH_BHPAR8_OFFSET)
#define	BCH_BHPAR9    	(BCH_BASE + BCH_BHPAR9_OFFSET)
#define	BCH_BHERR0      (BCH_BASE + BCH_BHERR0_OFFSET)
#define	BCH_BHERR1      (BCH_BASE + BCH_BHERR1_OFFSET)
#define	BCH_BHERR2      (BCH_BASE + BCH_BHERR2_OFFSET)
#define	BCH_BHERR3      (BCH_BASE + BCH_BHERR3_OFFSET)
#define	BCH_BHERR4      (BCH_BASE + BCH_BHERR4_OFFSET)
#define	BCH_BHERR5      (BCH_BASE + BCH_BHERR5_OFFSET)
#define	BCH_BHERR6      (BCH_BASE + BCH_BHERR6_OFFSET)
#define	BCH_BHERR7      (BCH_BASE + BCH_BHERR7_OFFSET)
#define	BCH_BHERR8      (BCH_BASE + BCH_BHERR8_OFFSET)
#define	BCH_BHERR9      (BCH_BASE + BCH_BHERR9_OFFSET)
#define	BCH_BHERR10     (BCH_BASE + BCH_BHERR10_OFFSET)
#define	BCH_BHERR11     (BCH_BASE + BCH_BHERR11_OFFSET)
#define	BCH_BHINT    	(BCH_BASE + BCH_BHINT_OFFSET)
#define	BCH_BHINTES     (BCH_BASE + BCH_BHINTES_OFFSET)
#define	BCH_BHINTEC     (BCH_BASE + BCH_BHINTEC_OFFSET)
#define	BCH_BHINTE	(BCH_BASE + BCH_BHINTE_OFFSET)

/*
 * BCH registers common define
 */

/* BCH control register (BHCR) */
#define	BHCR_DMAE		BIT7  /* BCH DMA enable */
#define BHCR_ENCE		BIT2
#define	BHCR_BRST		BIT1  /* BCH reset */
#define	BHCR_BCHE		BIT0  /* BCH enable */

#define	BHCR_BSEL_LSB		3
#define	BHCR_BSEL_MASK		BITS_H2L(5, BHCR_BSEL_LSB)
 #define BHCR_BSEL(n)		(((n)/4 - 1) << BHCR_BSEL_LSB)	/* n = 4, 8, 12, 16, 20, 24 */

/* BCH interrupt status register (BHINT) */
#define BHINT_ALL_F		BIT4
#define	BHINT_DECF		BIT3
#define	BHINT_ENCF		BIT2
#define	BHINT_UNCOR		BIT1
#define	BHINT_ERR		BIT0

#define	BHINT_ERRC_LSB		27
#define	BHINT_ERRC_MASK		BITS_H2L(31, BHINT_ERRC_LSB)

/* BCH ENC/DEC count register (BHCNT) */
#define BHCNT_DEC_LSB		16
#define BHCNT_DEC_MASK		BITS_H2L(26, BHCNT_DEC_LSB)

#define BHCNT_ENC_LSB		0
#define BHCNT_ENC_MASK		BITS_H2L(10, BHCNT_ENC_LSB)

/* BCH error report register (BCHERR)*/
#define BCH_ERR_INDEX_LSB	0
#define BCH_ERR_INDEX_MASK	BITS_H2L(12, BCH_ERR_INDEX_LSB)

/* BCH common macro define */
#define BCH_ENCODE		1
#define BCH_DECODE		0

#ifndef __MIPS_ASSEMBLER

#define	REG_BCH_BHCR		REG32(BCH_BHCR)
#define	REG_BCH_BHCSR		REG32(BCH_BHCSR)
#define	REG_BCH_BHCCR		REG32(BCH_BHCCR)
#define	REG_BCH_BHCNT		REG32(BCH_BHCNT)
#define	REG_BCH_BHDR		REG8(BCH_BHDR)
#define	REG_BCH_BHPAR0		REG32(BCH_BHPAR0)
#define	REG_BCH_BHPAR1		REG32(BCH_BHPAR1)
#define	REG_BCH_BHPAR2		REG32(BCH_BHPAR2)
#define	REG_BCH_BHPAR3		REG32(BCH_BHPAR3)
#define	REG_BCH_BHPAR4		REG32(BCH_BHPAR4)
#define	REG_BCH_BHPAR5		REG32(BCH_BHPAR5)
#define	REG_BCH_BHPAR6		REG32(BCH_BHPAR6)
#define	REG_BCH_BHPAR7		REG32(BCH_BHPAR7)
#define	REG_BCH_BHPAR8		REG32(BCH_BHPAR8)
#define	REG_BCH_BHPAR9		REG32(BCH_BHPAR9)
#define	REG_BCH_BHERR0		REG32(BCH_BHERR0)
#define	REG_BCH_BHERR1		REG32(BCH_BHERR1)
#define	REG_BCH_BHERR2		REG32(BCH_BHERR2)
#define	REG_BCH_BHERR3		REG32(BCH_BHERR3)
#define	REG_BCH_BHERR4		REG32(BCH_BHERR4)
#define	REG_BCH_BHERR5		REG32(BCH_BHERR5)
#define	REG_BCH_BHERR6		REG32(BCH_BHERR6)
#define	REG_BCH_BHERR7		REG32(BCH_BHERR7)
#define	REG_BCH_BHERR8		REG32(BCH_BHERR8)
#define	REG_BCH_BHERR9		REG32(BCH_BHERR9)
#define	REG_BCH_BHERR10		REG32(BCH_BHERR10)
#define	REG_BCH_BHERR11		REG32(BCH_BHERR11)
#define	REG_BCH_BHINT		REG32(BCH_BHINT)
#define	REG_BCH_BHINTE		REG32(BCH_BHINTE)
#define	REG_BCH_BHINTEC		REG32(BCH_BHINTEC)
#define	REG_BCH_BHINTES		REG32(BCH_BHINTES)

#define __ecc_enable(encode, bit)			\
do {							\
	unsigned int tmp = BHCR_BRST | BHCR_BCHE;	\
	if (encode)					\
		tmp |= BHCR_ENCE;			\
	tmp |= BHCR_BSEL(bit);				\
	REG_BCH_BHCSR = tmp;				\
	REG_BCH_BHCCR = ~tmp;				\
} while (0)
#define __ecc_disable()		(REG_BCH_BHCCR = BHCR_BCHE)

#define __ecc_dma_enable()	(REG_BCH_BHCSR = BHCR_DMAE)
#define __ecc_dma_disable()	(REG_BCH_BHCCR = BHCR_DMAE)

#define __ecc_cnt_enc(n)	CMSREG32(BCH_BHCNT, (n) << BHCNT_ENC_LSB, BHCNT_ENC_MASK)
#define __ecc_cnt_dec(n)	CMSREG32(BCH_BHCNT, (n) << BHCNT_DEC_LSB, BHCNT_DEC_MASK)

#define __ecc_encode_sync()				\
do {							\
	unsigned int i = 1;				\
	while (!(REG_BCH_BHINT & BHINT_ENCF) && i++);	\
} while (0)

#define __ecc_decode_sync()				\
do {							\
	unsigned int i = 1;				\
	while (!(REG_BCH_BHINT & BHINT_DECF) && i++);	\
} while (0)

#endif /* __MIPS_ASSEMBLER */

#define BDMAC_BASE  0xB3450000

/*************************************************************************
 * BDMAC (BCH & NAND DMA Controller)
 *************************************************************************/

/* n is the DMA channel index (0 - 2) */
#define BDMAC_DSAR(n)		(BDMAC_BASE + (0x00 + (n) * 0x20)) /* DMA source address */
#define BDMAC_DTAR(n)  		(BDMAC_BASE + (0x04 + (n) * 0x20)) /* DMA target address */
#define BDMAC_DTCR(n)  		(BDMAC_BASE + (0x08 + (n) * 0x20)) /* DMA transfer count */
#define BDMAC_DRSR(n)  		(BDMAC_BASE + (0x0c + (n) * 0x20)) /* DMA request source */
#define BDMAC_DCCSR(n) 		(BDMAC_BASE + (0x10 + (n) * 0x20)) /* DMA control/status */
#define BDMAC_DCMD(n)  		(BDMAC_BASE + (0x14 + (n) * 0x20)) /* DMA command */
#define BDMAC_DDA(n)   		(BDMAC_BASE + (0x18 + (n) * 0x20)) /* DMA descriptor address */
#define BDMAC_DSD(n)   		(BDMAC_BASE + (0x1c + (n) * 0x20)) /* DMA Stride Address */
#define BDMAC_DNT(n)  		(BDMAC_BASE + (0xc0 + (n) * 0x04)) /* NAND Detect Timer */

#define BDMAC_DMACR		(BDMAC_BASE + 0x0300) 	/* DMA control register */
#define BDMAC_DMAIPR		(BDMAC_BASE + 0x0304) 	/* DMA interrupt pending */
#define BDMAC_DMADBR		(BDMAC_BASE + 0x0308) 	/* DMA doorbell */
#define BDMAC_DMADBSR		(BDMAC_BASE + 0x030C) 	/* DMA doorbell set */
#define BDMAC_DMACKE  		(BDMAC_BASE + 0x0310)
#define BDMAC_DMACKES  		(BDMAC_BASE + 0x0314)
#define BDMAC_DMACKEC  		(BDMAC_BASE + 0x0318)

#define REG_BDMAC_DSAR(n)	REG32(BDMAC_DSAR((n)))
#define REG_BDMAC_DTAR(n)	REG32(BDMAC_DTAR((n)))
#define REG_BDMAC_DTCR(n)	REG32(BDMAC_DTCR((n)))
#define REG_BDMAC_DRSR(n)	REG32(BDMAC_DRSR((n)))
#define REG_BDMAC_DCCSR(n)	REG32(BDMAC_DCCSR((n)))
#define REG_BDMAC_DCMD(n)	REG32(BDMAC_DCMD((n)))
#define REG_BDMAC_DDA(n)	REG32(BDMAC_DDA((n)))
#define REG_BDMAC_DSD(n)    REG32(BDMAC_DSD(n))
#define REG_BDMAC_DNT(n)	REG32(BDMAC_DNT(n))

#define REG_BDMAC_DMACR		REG32(BDMAC_DMACR)
#define REG_BDMAC_DMAIPR	REG32(BDMAC_DMAIPR)
#define REG_BDMAC_DMADBR	REG32(BDMAC_DMADBR)
#define REG_BDMAC_DMADBSR	REG32(BDMAC_DMADBSR)
#define REG_BDMAC_DMACKE    	REG32(BDMAC_DMACKE)
#define REG_BDMAC_DMACKES	REG32(BDMAC_DMACKES)
#define REG_BDMAC_DMACKEC	REG32(BDMAC_DMACKEC)

// BDMA request source register
#define BDMAC_DRSR_RS_BIT	0
  #define BDMAC_DRSR_RS_MASK	(0x3f << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_BCH_ENC	(2 << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_BCH_DEC	(3 << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_NAND0	(6 << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_NAND1	(7 << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_NAND 	(BDMAC_DRSR_RS_NAND0)
  #define BDMAC_DRSR_RS_AUTO	(8 << DMAC_DRSR_RS_BIT)
  #define BDMAC_DRSR_RS_EXT	(12 << DMAC_DRSR_RS_BIT)

// BDMA channel control/status register
#define BDMAC_DCCSR_NDES	(1 << 31) /* descriptor (0) or not (1) ? */
#define BDMAC_DCCSR_DES8    	(1 << 30) /* Descriptor 8 Word */
#define BDMAC_DCCSR_DES4    	(0 << 30) /* Descriptor 4 Word */
#define BDMAC_DCCSR_LASTMD_BIT	28
#define BDMAC_DCCSR_LASTMD_MASK	(0x3 << BDMAC_DCCSR_LASTMD_BIT)
#define BDMAC_DCCSR_LASTMD0    	(0 << 28) /* BCH Decoding last mode 0, there's one descriptor for decoding blcok*/
#define BDMAC_DCCSR_LASTMD1    	(1 << 28) /* BCH Decoding last mode 1, there's two descriptor for decoding blcok*/
#define BDMAC_DCCSR_LASTMD2    	(2 << 28) /* BCH Decoding last mode 2, there's three descriptor for decoding blcok*/
#define BDMAC_DCCSR_FRBS(n)	((n) << 24)
#define BDMAC_DCCSR_FRBS_BIT	24
#define BDMAC_DCCSR_FRBS_MASK	(0x7 << BDMAC_DCCSR_FRBS_BIT)
#define BDMAC_DCCSR_CDOA_BIT	16        /* copy of DMA offset address */
#define BDMAC_DCCSR_CDOA_MASK	(0xff << BDMACC_DCCSR_CDOA_BIT)
#define BDMAC_DCCSR_BERR_BIT	7
#define BDMAC_DCCSR_BERR_MASK	(0x1f << BDMAC_DCCSR_BERR_BIT)
#define BDMAC_DCCSR_BUERR	(1 << 5)
#define BDMAC_DCCSR_AR		(1 << 4)  /* address error */
#define BDMAC_DCCSR_TT		(1 << 3)  /* transfer terminated */
#define BDMAC_DCCSR_HLT		(1 << 2)  /* DMA halted */
#define BDMAC_DCCSR_BAC		(1 << 1)
#define BDMAC_DCCSR_EN		(1 << 0)  /* channel enable bit */

// BDMA channel command register
#define BDMAC_DCMD_EACKS_LOW  	(1 << 31) /* External DACK Output Level Select, active low */
#define BDMAC_DCMD_EACKS_HIGH  	(0 << 31) /* External DACK Output Level Select, active high */
#define BDMAC_DCMD_EACKM_WRITE 	(1 << 30) /* External DACK Output Mode Select, output in write cycle */
#define BDMAC_DCMD_EACKM_READ 	(0 << 30) /* External DACK Output Mode Select, output in read cycle */
#define BDMAC_DCMD_ERDM_BIT	28        /* External DREQ Detection Mode Select */
  #define BDMAC_DCMD_ERDM_MASK	(0x03 << BDMAC_DCMD_ERDM_BIT)
  #define BDMAC_DCMD_ERDM_LOW	(0 << BDMAC_DCMD_ERDM_BIT)
  #define BDMAC_DCMD_ERDM_FALL	(1 << BDMAC_DCMD_ERDM_BIT)
  #define BDMAC_DCMD_ERDM_HIGH	(2 << BDMAC_DCMD_ERDM_BIT)
  #define BDMAC_DCMD_ERDM_RISE	(3 << BDMAC_DCMD_ERDM_BIT)
#define BDMAC_DCMD_BLAST	(1 << 25) /* BCH last */
#define BDMAC_DCMD_SAI		(1 << 23) /* source address increment */
#define BDMAC_DCMD_DAI		(1 << 22) /* dest address increment */
#define BDMAC_DCMD_SWDH_BIT	14  /* source port width */
  #define BDMAC_DCMD_SWDH_MASK	(0x03 << BDMAC_DCMD_SWDH_BIT)
  #define BDMAC_DCMD_SWDH_32	(0 << BDMAC_DCMD_SWDH_BIT)
  #define BDMAC_DCMD_SWDH_8	(1 << BDMAC_DCMD_SWDH_BIT)
  #define BDMAC_DCMD_SWDH_16	(2 << BDMAC_DCMD_SWDH_BIT)
#define BDMAC_DCMD_DWDH_BIT	12  /* dest port width */
  #define BDMAC_DCMD_DWDH_MASK	(0x03 << BDMAC_DCMD_DWDH_BIT)
  #define BDMAC_DCMD_DWDH_32	(0 << BDMAC_DCMD_DWDH_BIT)
  #define BDMAC_DCMD_DWDH_8	(1 << BDMAC_DCMD_DWDH_BIT)
  #define BDMAC_DCMD_DWDH_16	(2 << BDMAC_DCMD_DWDH_BIT)
#define BDMAC_DCMD_DS_BIT	8  /* transfer data size of a data unit */
  #define BDMAC_DCMD_DS_MASK	(0x07 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_32BIT	(0 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_8BIT	(1 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_16BIT	(2 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_16BYTE	(3 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_32BYTE	(4 << BDMAC_DCMD_DS_BIT)
  #define BDMAC_DCMD_DS_64BYTE	(5 << BDMAC_DCMD_DS_BIT)
#define BDMAC_DCMD_NRD   	(1 << 7)  /* NAND direct read */
#define BDMAC_DCMD_NWR   	(1 << 6)  /* NAND direct write */
#define BDMAC_DCMD_NAC   	(1 << 5)  /* NAND AL/CL enable */
#define BDMAC_DCMD_NSTA   	(1 << 4)
#define BDMAC_DCMD_STDE   	(1 << 2)  /* Stride Disable/Enable */
#define BDMAC_DCMD_TIE		(1 << 1)  /* DMA transfer interrupt enable */
#define BDMAC_DCMD_LINK		(1 << 0)  /* descriptor link enable */

// BDMA descriptor address register
#define BDMAC_DDA_BASE_BIT	12  /* descriptor base address */
  #define BDMAC_DDA_BASE_MASK	(0x0fffff << BDMAC_DDA_BASE_BIT)
#define BDMAC_DDA_OFFSET_BIT	4   /* descriptor offset address */
  #define BDMAC_DDA_OFFSET_MASK	(0x0ff << BDMAC_DDA_OFFSET_BIT)

// BDMA stride address register
#define BDMAC_DSD_TSD_BIT	16	/* target stride address */
  #define BDMAC_DSD_TSD_MASK	(0xffff << BDMAC_DSD_TSD_BIT)
#define BDMAC_DSD_SSD_BIT	0	/* source stride address */
  #define BDMAC_DSD_SSD_MASK	(0xffff << BDMAC_DSD_SSD_BIT)

// BDMA NAND Detect timer register
#define BDMAC_NDTCTIMER_EN	(1 << 15)  /* enable detect timer */
#define BDMAC_TAILCNT_BIT	16

// BDMA control register
#define BDMAC_DMACR_PR_BIT	8	/* channel priority mode */
  #define BDMAC_DMACR_PR_MASK	(0x03 << DMAC_DMACR_PR_BIT)
  #define BDMAC_DMACR_PR_01_2	(0 << BDMAC_DMACR_PR_BIT)
  #define BDMAC_DMACR_PR_12_0	(1 << BDMAC_DMACR_PR_BIT)
  #define BDMAC_DMACR_PR_20_1	(2 << BDMAC_DMACR_PR_BIT)
  #define BDMAC_DMACR_PR_012	(3 << BDMAC_DMACR_PR_BIT)
#define BDMAC_DMACR_HLT		(1 << 3)  /* DMA halt flag */
#define BDMAC_DMACR_AR		(1 << 2)  /* address error flag */
#define BDMAC_DMACR_DMAE	(1 << 0)  /* DMA enable bit */

// BDMA interrupt pending register
#define BDMAC_DMAIPR_CIRQ2	(1 << 2)  /* irq pending status for channel 2 */
#define BDMAC_DMAIPR_CIRQ1	(1 << 1)  /* irq pending status for channel 1 */
#define BDMAC_DMAIPR_CIRQ0	(1 << 0)  /* irq pending status for channel 0 */

// BDMA doorbell register
#define BDMAC_DMADBR_DB2	(1 << 2)  /* doorbell for channel 2 */
#define BDMAC_DMADBR_DB1	(1 << 1)  /* doorbell for channel 1 */
#define BDMAC_DMADBR_DB0	(1 << 0)  /* doorbell for channel 0 */

// BDMA doorbell set register
#define BDMAC_DMADBSR_DBS2	(1 << 2)  /* enable doorbell for channel 2 */
#define BDMAC_DMADBSR_DBS1	(1 << 1)  /* enable doorbell for channel 1 */
#define BDMAC_DMADBSR_DBS0	(1 << 0)  /* enable doorbell for channel 0 */

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * BCH & NAND DMAC
 ***************************************************************************/

#define __bdmac_enable_module() \
	( REG_BDMAC_DMACR |= BDMAC_DMACR_DMAE )
#define __bdmac_disable_module() \
	( REG_BDMAC_DMACR &= ~BDMAC_DMACR_DMAE )

/* n is the DMA channel index (0 - 2) */

#define __bdmac_test_halt_error ( REG_BDMAC_DMACR & BDMAC_DMACR_HLT )
#define __bdmac_test_addr_error ( REG_BDMAC_DMACR & BDMAC_DMACR_AR )

#define __bdmac_channel_enable_clk(n)           \
	REG_BDMAC_DMACKES = 1 << (n);

#define __bdmac_enable_descriptor(n) \
  ( REG_BDMAC_DCCSR((n)) &= ~BDMAC_DCCSR_NDES )
#define __bdmac_disable_descriptor(n) \
  ( REG_BDMAC_DCCSR((n)) |= BDMAC_DCCSR_NDES )

#define __bdmac_enable_channel(n)                 \
do {                                             \
	REG_BDMAC_DCCSR((n)) |= BDMAC_DCCSR_EN;    \
} while (0)
#define __bdmac_disable_channel(n)                \
do {                                             \
	REG_BDMAC_DCCSR((n)) &= ~BDMAC_DCCSR_EN;   \
} while (0)

#define __bdmac_channel_enable_irq(n) \
  ( REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_TIE )
#define __bdmac_channel_disable_irq(n) \
  ( REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_TIE )

#define __bdmac_channel_transmit_halt_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_HLT )
#define __bdmac_channel_transmit_end_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_TT )
/* Nand ops status error, only for channel 1 */
#define __bdmac_channel_status_error_detected() \
  (  REG_BDMAC_DCCSR(1) & BDMAC_DCCSR_NSERR )
#define __bdmac_channel_address_error_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_AR )
#define __bdmac_channel_count_terminated_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_CT )
#define __bdmac_channel_descriptor_invalid_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_INV )
#define __bdmac_BCH_error_detected(n) \
  (  REG_BDMAC_DCCSR((n)) & BDMAC_DCCSR_BERR )

#define __bdmac_channel_clear_transmit_halt(n)				\
	do {								\
		/* clear both channel halt error and globle halt error */ \
		REG_BDMAC_DCCSR(n) &= ~BDMAC_DCCSR_HLT;			\
		REG_BDMAC_DMACR &= ~BDMAC_DMACR_HLT;	\
	} while (0)
#define __bdmac_channel_clear_transmit_end(n) \
  (  REG_BDMAC_DCCSR(n) &= ~BDMAC_DCCSR_TT )
/* Nand ops status error, only for channel 1 */
#define __bdmac_channel_clear_status_error() \
  ( REG_BDMAC_DCCSR(1) &= ~BDMAC_DCCSR_NSERR )
#define __bdmac_channel_clear_address_error(n)				\
	do {								\
		REG_BDMAC_DDA(n) = 0; /* clear descriptor address register */ \
		REG_BDMAC_DSAR(n) = 0; /* clear source address register */ \
		REG_BDMAC_DTAR(n) = 0; /* clear target address register */ \
		/* clear both channel addr error and globle address error */ \
		REG_BDMAC_DCCSR(n) &= ~BDMAC_DCCSR_AR;			\
		REG_BDMAC_DMACR &= ~BDMAC_DMACR_AR;	\
	} while (0)
#define __bdmac_channel_clear_count_terminated(n) \
  (  REG_BDMAC_DCCSR((n)) &= ~BDMAC_DCCSR_CT )
#define __bdmac_channel_clear_descriptor_invalid(n) \
  (  REG_BDMAC_DCCSR((n)) &= ~BDMAC_DCCSR_INV )

#define __bdmac_channel_set_transfer_unit_32bit(n)	\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DS_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DS_32BIT;	\
} while (0)

#define __bdmac_channel_set_transfer_unit_16bit(n)	\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DS_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DS_16BIT;	\
} while (0)

#define __bdmac_channel_set_transfer_unit_8bit(n)	\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DS_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DS_8BIT;	\
} while (0)

#define __bdmac_channel_set_transfer_unit_16byte(n)	\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DS_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DS_16BYTE;	\
} while (0)

#define __bdmac_channel_set_transfer_unit_32byte(n)	\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DS_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DS_32BYTE;	\
} while (0)

/* w=8,16,32 */
#define __bdmac_channel_set_dest_port_width(n,w)		\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DWDH_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DWDH_##w;	\
} while (0)

/* w=8,16,32 */
#define __bdmac_channel_set_src_port_width(n,w)		\
do {							\
	REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_SWDH_MASK;	\
	REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_SWDH_##w;	\
} while (0)

#define __bdmac_channel_dest_addr_fixed(n) \
	(REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_DAI)
#define __bdmac_channel_dest_addr_increment(n) \
	(REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_DAI)

#define __bdmac_channel_src_addr_fixed(n) \
	(REG_BDMAC_DCMD((n)) &= ~BDMAC_DCMD_SAI)
#define __bdmac_channel_src_addr_increment(n) \
	(REG_BDMAC_DCMD((n)) |= BDMAC_DCMD_SAI)

#define __bdmac_channel_set_doorbell(n)	\
	(REG_BDMAC_DMADBSR = (1 << (n)))

#define __bdmac_channel_irq_detected(n)  (REG_BDMAC_DMAIPR & (1 << (n)))
#define __bdmac_channel_ack_irq(n)       (REG_BDMAC_DMAIPR &= ~(1 <<(n)))

static __inline__ int __bdmac_get_irq(void)
{
	int i;
	for (i = 0; i < MAX_BDMA_NUM; i++)
		if (__bdmac_channel_irq_detected(i))
			return i;
	return -1;
}

#endif /* __MIPS_ASSEMBLER */

#define	CIM_BASE	0xB3060000

/*************************************************************************
 * CIM
 *************************************************************************/
#define	CIM_CFG			(CIM_BASE + 0x0000)
#define	CIM_CTRL		(CIM_BASE + 0x0004)
#define	CIM_STATE		(CIM_BASE + 0x0008)
#define	CIM_IID			(CIM_BASE + 0x000C)
#define	CIM_DA			(CIM_BASE + 0x0020)
#define	CIM_FA			(CIM_BASE + 0x0024)
#define	CIM_FID			(CIM_BASE + 0x0028)
#define	CIM_CMD			(CIM_BASE + 0x002C)
#define	CIM_SIZE		(CIM_BASE + 0x0030)
#define	CIM_OFFSET		(CIM_BASE + 0x0034)
#define CIM_YFA			(CIM_BASE + 0x0038)
#define CIM_YCMD		(CIM_BASE + 0x003C)
#define CIM_CBFA		(CIM_BASE + 0x0040)
#define CIM_CBCMD		(CIM_BASE + 0x0044)
#define CIM_CRFA		(CIM_BASE + 0x0048)
#define CIM_CRCMD		(CIM_BASE + 0x004C)
#define CIM_CTRL2		(CIM_BASE + 0x0050)

#define	CIM_RAM_ADDR		(CIM_BASE + 0x1000)

#define	REG_CIM_CFG		REG32(CIM_CFG)
#define	REG_CIM_CTRL		REG32(CIM_CTRL)
#define	REG_CIM_STATE		REG32(CIM_STATE)
#define	REG_CIM_IID		REG32(CIM_IID)
#define	REG_CIM_DA		REG32(CIM_DA)
#define	REG_CIM_FA		REG32(CIM_FA)
#define	REG_CIM_FID		REG32(CIM_FID)
#define	REG_CIM_CMD		REG32(CIM_CMD)
#define	REG_CIM_SIZE		REG32(CIM_SIZE)
#define	REG_CIM_OFFSET		REG32(CIM_OFFSET)
#define REG_CIM_YFA		REG32(CIM_YFA)
#define REG_CIM_YCMD		REG32(CIM_YCMD)
#define REG_CIM_CBFA		REG32(CIM_CBFA)
#define REG_CIM_CBCMD		REG32(CIM_CBCMD)
#define REG_CIM_CRFA		REG32(CIM_CRFA)
#define REG_CIM_CRCMD		REG32(CIM_CRCMD)
#define	REG_CIM_CTRL2		REG32(CIM_CTRL2)

#define CIM_CFG_EEOFEN			(1 << 31)
#define CIM_CFG_EXP			(1 << 30)

#define CIM_CFG_RXF_TRIG_BIT		24
#define CIM_CFG_RXF_TRIG_MASK		(0x3f << CIM_CFG_RXF_TRIG_BIT)

#define CIM_CFG_BW_BIT			22
#define CIM_CFG_BW_MASK			(0x3 << CIM_CFG_BW_BIT)

#define CIM_CFG_SEP			(1 << 20)

#define	CIM_CFG_ORDER_BIT		18
#define	CIM_CFG_ORDER_MASK		(0x3 << CIM_CFG_ORDER_BIT)
#define CIM_CFG_ORDER_0	  		(0x0 << CIM_CFG_ORDER_BIT) 	/* Y0CbY1Cr; YCbCr */
#define CIM_CFG_ORDER_1	  		(0x1 << CIM_CFG_ORDER_BIT)	/* Y0CrY1Cb; YCrCb */
#define CIM_CFG_ORDER_2	  		(0x2 << CIM_CFG_ORDER_BIT)	/* CbY0CrY1; CbCrY */
#define CIM_CFG_ORDER_3	  		(0x3 << CIM_CFG_ORDER_BIT)	/* CrY0CbY1; CrCbY */

#define	CIM_CFG_DF_BIT			16
#define	CIM_CFG_DF_MASK			(0x3 << CIM_CFG_DF_BIT)
#define CIM_CFG_DF_YUV444		(0x1 << CIM_CFG_DF_BIT) 	/* YCbCr444 */
#define CIM_CFG_DF_YUV422		(0x2 << CIM_CFG_DF_BIT)	/* YCbCr422 */
#define CIM_CFG_DF_ITU656		(0x3 << CIM_CFG_DF_BIT)	/* ITU656 YCbCr422 */

#define	CIM_CFG_INV_DAT			(1 << 15)
#define	CIM_CFG_VSP			(1 << 14) /* VSYNC Polarity:0-rising edge active,1-falling edge active */
#define	CIM_CFG_HSP			(1 << 13) /* HSYNC Polarity:0-rising edge active,1-falling edge active */
#define	CIM_CFG_PCP			(1 << 12) /* PCLK working edge: 0-rising, 1-falling */

#define	CIM_CFG_DMA_BURST_TYPE_BIT	10
#define	CIM_CFG_DMA_BURST_TYPE_MASK	(0x3 << CIM_CFG_DMA_BURST_TYPE_BIT)
#define	CIM_CFG_DMA_BURST_INCR4		(0 << CIM_CFG_DMA_BURST_TYPE_BIT)
#define	CIM_CFG_DMA_BURST_INCR8		(1 << CIM_CFG_DMA_BURST_TYPE_BIT)	/* Suggested */
#define	CIM_CFG_DMA_BURST_INCR16	(2 << CIM_CFG_DMA_BURST_TYPE_BIT)	/* Suggested High speed AHB*/
#define	CIM_CFG_DMA_BURST_INCR32	(3 << CIM_CFG_DMA_BURST_TYPE_BIT)	/* Suggested High speed AHB*/

#define	CIM_CFG_DUMMY_ZERO		(1 << 9)
#define	CIM_CFG_EXT_VSYNC		(1 << 8)	/* Only for ITU656 Progressive mode */
#define	CIM_CFG_LM			(1 << 7)	/* Only for ITU656 Progressive mode */
#define	CIM_CFG_PACK_BIT		4
#define	CIM_CFG_PACK_MASK		(0x7 << CIM_CFG_PACK_BIT)
#define CIM_CFG_PACK_0	  		(0 << CIM_CFG_PACK_BIT) /* 11 22 33 44 0xY0CbY1Cr */
#define CIM_CFG_PACK_1	  		(1 << CIM_CFG_PACK_BIT) /* 22 33 44 11 0xCbY1CrY0 */
#define CIM_CFG_PACK_2	  		(2 << CIM_CFG_PACK_BIT) /* 33 44 11 22 0xY1CrY0Cb */
#define CIM_CFG_PACK_3	  		(3 << CIM_CFG_PACK_BIT) /* 44 11 22 33 0xCrY0CbY1 */
#define CIM_CFG_PACK_4	  		(4 << CIM_CFG_PACK_BIT) /* 44 33 22 11 0xCrY1CbY0 */
#define CIM_CFG_PACK_5	  		(5 << CIM_CFG_PACK_BIT) /* 33 22 11 44 0xY1CbY0Cr */
#define CIM_CFG_PACK_6	  		(6 << CIM_CFG_PACK_BIT) /* 22 11 44 33 0xCbY0CrY1 */
#define CIM_CFG_PACK_7	  		(7 << CIM_CFG_PACK_BIT) /* 11 44 33 22 0xY0CrY1Cb */
#define	CIM_CFG_FP			(1 << 3)	/* Only for ITU656 Progressive mode */
#define	CIM_CFG_BYPASS_BIT		2
#define	CIM_CFG_BYPASS_MASK		(1 << CIM_CFG_BYPASS_BIT)
#define CIM_CFG_BYPASS	  		(1 << CIM_CFG_BYPASS_BIT)
#define	CIM_CFG_DSM_BIT			0
#define	CIM_CFG_DSM_MASK		(0x3 << CIM_CFG_DSM_BIT)
#define CIM_CFG_DSM_CPM	  		(0 << CIM_CFG_DSM_BIT) /* CCIR656 Progressive Mode */
#define CIM_CFG_DSM_CIM	  		(1 << CIM_CFG_DSM_BIT) /* CCIR656 Interlace Mode */
#define CIM_CFG_DSM_GCM	  		(2 << CIM_CFG_DSM_BIT) /* Gated Clock Mode */

/* CIM Control Register  (CIM_CTRL) */
#define	CIM_CTRL_EEOF_LINE_BIT	20
#define	CIM_CTRL_EEOF_LINE_MASK	(0xfff << CIM_CTRL_EEOF_LINE_BIT)

#define	CIM_CTRL_FRC_BIT	16
#define	CIM_CTRL_FRC_MASK	(0xf << CIM_CTRL_FRC_BIT)
#define CIM_CTRL_FRC_1	  (0x0 << CIM_CTRL_FRC_BIT) /* Sample every frame */
#define CIM_CTRL_FRC_2	  (0x1 << CIM_CTRL_FRC_BIT) /* Sample 1/2 frame */
#define CIM_CTRL_FRC_3	  (0x2 << CIM_CTRL_FRC_BIT) /* Sample 1/3 frame */
#define CIM_CTRL_FRC_4	  (0x3 << CIM_CTRL_FRC_BIT) /* Sample 1/4 frame */
#define CIM_CTRL_FRC_5	  (0x4 << CIM_CTRL_FRC_BIT) /* Sample 1/5 frame */
#define CIM_CTRL_FRC_6	  (0x5 << CIM_CTRL_FRC_BIT) /* Sample 1/6 frame */
#define CIM_CTRL_FRC_7	  (0x6 << CIM_CTRL_FRC_BIT) /* Sample 1/7 frame */
#define CIM_CTRL_FRC_8	  (0x7 << CIM_CTRL_FRC_BIT) /* Sample 1/8 frame */
#define CIM_CTRL_FRC_9	  (0x8 << CIM_CTRL_FRC_BIT) /* Sample 1/9 frame */
#define CIM_CTRL_FRC_10	  (0x9 << CIM_CTRL_FRC_BIT) /* Sample 1/10 frame */
#define CIM_CTRL_FRC_11	  (0xA << CIM_CTRL_FRC_BIT) /* Sample 1/11 frame */
#define CIM_CTRL_FRC_12	  (0xB << CIM_CTRL_FRC_BIT) /* Sample 1/12 frame */
#define CIM_CTRL_FRC_13	  (0xC << CIM_CTRL_FRC_BIT) /* Sample 1/13 frame */
#define CIM_CTRL_FRC_14	  (0xD << CIM_CTRL_FRC_BIT) /* Sample 1/14 frame */
#define CIM_CTRL_FRC_15	  (0xE << CIM_CTRL_FRC_BIT) /* Sample 1/15 frame */
#define CIM_CTRL_FRC_16	  (0xF << CIM_CTRL_FRC_BIT) /* Sample 1/16 frame */

#define	CIM_CTRL_DMA_EEOF	(1 << 15)	/* Enable EEOF interrupt */
#define	CIM_CTRL_WIN_EN		(1 << 14)
#define	CIM_CTRL_VDDM		(1 << 13) /* VDD interrupt enable */
#define	CIM_CTRL_DMA_SOFM	(1 << 12)
#define	CIM_CTRL_DMA_EOFM	(1 << 11)
#define	CIM_CTRL_DMA_STOPM	(1 << 10)
#define	CIM_CTRL_RXF_TRIGM	(1 << 9)
#define	CIM_CTRL_RXF_OFM	(1 << 8)
#define	CIM_CTRL_DMA_SYNC	(1 << 7)	/*when change DA, do frame sync */
#define CIM_CTRL_H_SYNC         (1 << 6) /*Enable horizental sync when CIMCFG.SEP is 1*/

#define	CIM_CTRL_PPW_BIT	3
#define	CIM_CTRL_PPW_MASK	(0x3 << CIM_CTRL_PPW_BIT)

#define	CIM_CTRL_DMA_EN		(1 << 2) /* Enable DMA */
#define	CIM_CTRL_RXF_RST	(1 << 1) /* RxFIFO reset */
#define	CIM_CTRL_ENA		(1 << 0) /* Enable CIM */

/* cim control2 */
#define CIM_CTRL2_OPG_BIT	4
#define CIM_CTRL2_OPG_MASK	(0x3 << CIM_CTRL2_OPG_BIT)
#define CIM_CTRL2_OPE		(1 << 2)
#define CIM_CTRL2_EME		(1 << 1)
#define CIM_CTRL2_APM		(1 << 0)

/* CIM State Register  (CIM_STATE) */
#define CIM_STATE_CR_RF_OF	(1 << 27)
#define CIM_STATE_CR_RF_TRIG	(1 << 26)
#define CIM_STATE_CR_RF_EMPTY	(1 << 25)
#define CIM_STATE_CB_RF_OF	(1 << 19)
#define CIM_STATE_CB_RF_TRIG	(1 << 18)
#define CIM_STATE_CB_RF_EMPTY	(1 << 17)
#define CIM_STATE_Y_RF_OF	(1 << 11)
#define CIM_STATE_Y_RF_TRIG	(1 << 10)
#define CIM_STATE_Y_RF_EMPTY	(1 << 9)
#define	CIM_STATE_DMA_EEOF	(1 << 7) /* DMA Line EEOf irq */
#define	CIM_STATE_DMA_SOF	(1 << 6) /* DMA start irq */
#define	CIM_STATE_DMA_EOF	(1 << 5) /* DMA end irq */
#define	CIM_STATE_DMA_STOP	(1 << 4) /* DMA stop irq */
#define	CIM_STATE_RXF_OF	(1 << 3) /* RXFIFO over flow irq */
#define	CIM_STATE_RXF_TRIG	(1 << 2) /* RXFIFO triger meet irq */
#define	CIM_STATE_RXF_EMPTY	(1 << 1) /* RXFIFO empty irq */
#define	CIM_STATE_VDD		(1 << 0) /* CIM disabled irq */

/* CIM DMA Command Register (CIM_CMD) */

#define	CIM_CMD_SOFINT		(1 << 31) /* enable DMA start irq */
#define	CIM_CMD_EOFINT		(1 << 30) /* enable DMA end irq */
#define	CIM_CMD_EEOFINT		(1 << 29) /* enable DMA EEOF irq */
#define	CIM_CMD_STOP		(1 << 28) /* enable DMA stop irq */
#define	CIM_CMD_OFRCV		(1 << 27) /* enable recovery when TXFiFo overflow */
#define	CIM_CMD_LEN_BIT		0
#define	CIM_CMD_LEN_MASK	(0xffffff << CIM_CMD_LEN_BIT)

/* CIM Window-Image Size Register  (CIM_SIZE) */
#define	CIM_SIZE_LPF_BIT	16 /* Lines per freame for csc output image */
#define	CIM_SIZE_LPF_MASK	(0x1fff << CIM_SIZE_LPF_BIT)
#define	CIM_SIZE_PPL_BIT	0 /* Pixels per line for csc output image, should be an even number */
#define	CIM_SIZE_PPL_MASK	(0x1fff << CIM_SIZE_PPL_BIT)

/* CIM Image Offset Register  (CIM_OFFSET) */
#define	CIM_OFFSET_V_BIT	16 /* Vertical offset */
#define	CIM_OFFSET_V_MASK	(0xfff << CIM_OFFSET_V_BIT)
#define	CIM_OFFSET_H_BIT	0 /* Horizontal offset, should be an enen number */
#define	CIM_OFFSET_H_MASK	(0xfff << CIM_OFFSET_H_BIT) /*OFFSET_H should be even number*/

#define	CIM_YCMD_SOFINT		(1 << 31) /* enable DMA start irq */
#define	CIM_YCMD_EOFINT		(1 << 30) /* enable DMA end irq */
#define	CIM_YCMD_EEOFINT	(1 << 29) /* enable DMA EEOF irq */
#define	CIM_YCMD_STOP		(1 << 28) /* enable DMA stop irq */
#define	CIM_YCMD_OFRCV		(1 << 27) /* enable recovery when TXFiFo overflow */
#define	CIM_YCMD_LEN_BIT	0
#define	CIM_YCMD_LEN_MASK	(0xffffff << CIM_YCMD_LEN_BIT)

#define	CIM_CBCMD_LEN_BIT	0
#define	CIM_CBCMD_LEN_MASK	(0xffffff << CIM_CBCMD_LEN_BIT)

#define	CIM_CRCMD_LEN_BIT	0
#define	CIM_CRCMD_LEN_MASK	(0xffffff << CIM_CRCMD_LEN_BIT)

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * CIM
 ***************************************************************************/

#define __cim_enable()	( REG_CIM_CTRL |= CIM_CTRL_ENA )
#define __cim_disable()	( REG_CIM_CTRL &= ~CIM_CTRL_ENA )

#define __cim_enable_sep() (REG_CIM_CFG |= CIM_CFG_SEP)
#define __cim_disable_sep() (REG_CIM_CFG &= ~CIM_CFG_SEP)

/* n = 0, 1, 2, 3 */
#define __cim_set_input_data_stream_order(n)				\
	do {								\
		REG_CIM_CFG &= ~CIM_CFG_ORDER_MASK;			\
		REG_CIM_CFG |= ((n)<<CIM_CFG_ORDER_BIT)&CIM_CFG_ORDER_MASK; \
	} while (0)

#define __cim_input_data_format_select_YUV444()		\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DF_MASK;		\
		REG_CIM_CFG |= CIM_CFG_DF_YUV444;	\
	} while (0)

#define __cim_input_data_format_select_YUV422()		\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DF_MASK;		\
		REG_CIM_CFG |= CIM_CFG_DF_YUV422;	\
	} while (0)

#define __cim_input_data_format_select_ITU656()		\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DF_MASK;		\
		REG_CIM_CFG |= CIM_CFG_DF_ITU656;	\
	} while (0)

#define __cim_input_data_inverse()	( REG_CIM_CFG |= CIM_CFG_INV_DAT )
#define __cim_input_data_normal()	( REG_CIM_CFG &= ~CIM_CFG_INV_DAT )

#define __cim_vsync_active_low()	( REG_CIM_CFG |= CIM_CFG_VSP )
#define __cim_vsync_active_high()	( REG_CIM_CFG &= ~CIM_CFG_VSP )

#define __cim_hsync_active_low()	( REG_CIM_CFG |= CIM_CFG_HSP )
#define __cim_hsync_active_high()	( REG_CIM_CFG &= ~CIM_CFG_HSP )

#define __cim_sample_data_at_pclk_falling_edge() \
	( REG_CIM_CFG |= CIM_CFG_PCP )
#define __cim_sample_data_at_pclk_rising_edge() \
	( REG_CIM_CFG &= ~CIM_CFG_PCP )

#define __cim_enable_dummy_zero()	( REG_CIM_CFG |= CIM_CFG_DUMMY_ZERO )
#define __cim_disable_dummy_zero()	( REG_CIM_CFG &= ~CIM_CFG_DUMMY_ZERO )

#define __cim_select_external_vsync()	( REG_CIM_CFG |= CIM_CFG_EXT_VSYNC )
#define __cim_select_internal_vsync()	( REG_CIM_CFG &= ~CIM_CFG_EXT_VSYNC )

/* n=0-7 */
#define __cim_set_data_packing_mode(n) 		\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_PACK_MASK;	\
		REG_CIM_CFG |= (CIM_CFG_PACK_##n);	\
	} while (0)

#define __cim_enable_bypass_func() 	(REG_CIM_CFG &= ~CIM_CFG_BYPASS)
#define __cim_disable_bypass_func() 	(REG_CIM_CFG |= CIM_CFG_BYPASS)

#define __cim_enable_ccir656_progressive_mode()	\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DSM_MASK;	\
		REG_CIM_CFG |= CIM_CFG_DSM_CPM;		\
	} while (0)

#define __cim_enable_ccir656_interlace_mode()	\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DSM_MASK;	\
		REG_CIM_CFG |= CIM_CFG_DSM_CIM;		\
	} while (0)

#define __cim_enable_gated_clock_mode()		\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DSM_MASK;	\
		REG_CIM_CFG |= CIM_CFG_DSM_GCM;		\
	} while (0)

#define __cim_enable_nongated_clock_mode()	\
	do {						\
		REG_CIM_CFG &= ~CIM_CFG_DSM_MASK;	\
		REG_CIM_CFG |= CIM_CFG_DSM_NGCM;	\
	} while (0)

/* n=1-16 */
#define __cim_set_frame_rate(n) 		\
	do {						\
		REG_CIM_CTRL &= ~CIM_CTRL_FRC_MASK; 	\
		REG_CIM_CTRL |= CIM_CTRL_FRC_##n;	\
	} while (0)

#define __cim_enable_size_func() \
	( REG_CIM_CTRL |= CIM_CTRL_WIN_EN)
#define __cim_disable_size_func() \
	( REG_CIM_CTRL &= ~CIM_CTRL_WIN_EN )

#define __cim_enable_vdd_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_VDDM )
#define __cim_disable_vdd_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_VDDM )

#define __cim_enable_sof_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_DMA_SOFM )
#define __cim_disable_sof_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_DMA_SOFM )

#define __cim_enable_eof_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_DMA_EOFM )
#define __cim_disable_eof_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_DMA_EOFM )

#define __cim_enable_eeof_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_DMA_EEOFM )
#define __cim_disable_eeof_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_DMA_EEOFM )

#define __cim_enable_stop_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_DMA_STOPM )
#define __cim_disable_stop_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_DMA_STOPM )

#define __cim_enable_trig_intr() \
	( REG_CIM_CTRL |= CIM_CTRL_RXF_TRIGM )
#define __cim_disable_trig_intr() \
	( REG_CIM_CTRL &= ~CIM_CTRL_RXF_TRIGM )

#define __cim_enable_rxfifo_overflow_intr()	\
	( REG_CIM_CTRL |= CIM_CTRL_RXF_OFM )
#define __cim_disable_rxfifo_overflow_intr()	\
	( REG_CIM_CTRL &= ~CIM_CTRL_RXF_OFM )

#define __cim_enable_dma()   ( REG_CIM_CTRL |= CIM_CTRL_DMA_EN )
#define __cim_disable_dma()  ( REG_CIM_CTRL &= ~CIM_CTRL_DMA_EN )
#define __cim_reset_rxfifo() ( REG_CIM_CTRL |= CIM_CTRL_RXF_RST )
#define __cim_unreset_rxfifo() ( REG_CIM_CTRL &= ~CIM_CTRL_RXF_RST )

/* cim control2 */
#define __cim_enable_priority_control()		( REG_CIM_CTRL2 |= CIM_CTRL2_OPE)
#define __cim_disable_priority_control()	( REG_CIM_CTRL2 &= ~CIM_CTRL2_OPE)
#define __cim_enable_auto_priority()		( REG_CIM_CTRL2 |= CIM_CTRL2_APM)
#define __cim_disable_auto_priority()		( REG_CIM_CTRL2 &= ~CIM_CTRL2_APM)
#define __cim_enable_emergency()		( REG_CIM_CTRL2 |= CIM_CTRL2_EME)
#define __cim_disable_emergency()		( REG_CIM_CTRL2 &= ~CIM_CTRL2_EME);
/* 0, 1, 2, 3
 ** 0: highest priority
 ** 3: lowest priority
 ** 1 maybe best for SEP=1
 ** 3 maybe best for SEP=0
 **/
#define __cim_set_opg(n)				\
	do {								\
		REG_CIM_CTRL2 &= ~CIM_CTRL2_OPG_MASK;			\
		REG_CIM_CTRL2 |= ((n) << CIM_CTRL2_OPG_BIT) & CIM_CTRL2_OPG_MASK; \
	} while (0)

#define __cim_clear_state()   	     ( REG_CIM_STATE = 0 )

#define __cim_disable_done()   	     ( REG_CIM_STATE & CIM_STATE_VDD )
#define __cim_rxfifo_empty()   	     ( REG_CIM_STATE & CIM_STATE_RXF_EMPTY )
#define __cim_rxfifo_reach_trigger() ( REG_CIM_STATE & CIM_STATE_RXF_TRIG )
#define __cim_rxfifo_overflow()      ( REG_CIM_STATE & CIM_STATE_RXF_OF )
#define __cim_clear_rxfifo_overflow() ( REG_CIM_STATE &= ~CIM_STATE_RXF_OF )
#define __cim_dma_stop()   	     ( REG_CIM_STATE & CIM_STATE_DMA_STOP )
#define __cim_dma_eof()   	     ( REG_CIM_STATE & CIM_STATE_DMA_EOF )
#define __cim_dma_sof()   	     ( REG_CIM_STATE & CIM_STATE_DMA_SOF )

#define __cim_get_iid()   	     ( REG_CIM_IID )
#define __cim_get_fid()   	     ( REG_CIM_FID )
//#define __cim_get_image_data()       ( REG_CIM_RXFIFO )
#define __cim_get_dma_cmd()          ( REG_CIM_CMD )

#define __cim_set_da(a)              ( REG_CIM_DA = (a) )

#define __cim_set_line(a) 	( REG_CIM_SIZE = (REG_CIM_SIZE&(~CIM_SIZE_LPF_MASK))|((a)<<CIM_SIZE_LPF_BIT) )
#define __cim_set_pixel(a) 	( REG_CIM_SIZE = (REG_CIM_SIZE&(~CIM_SIZE_PPL_MASK))|((a)<<CIM_SIZE_PPL_BIT) )
#define __cim_get_line() 	((REG_CIM_SIZE&CIM_SIZE_LPF_MASK)>>CIM_SIZE_LPF_BIT)
#define __cim_get_pixel() 	((REG_CIM_SIZE&CIM_SIZE_PPL_MASK)>>CIM_SIZE_PPL_BIT)

#define __cim_set_v_offset(a) 	( REG_CIM_OFFSET = (REG_CIM_OFFSET&(~CIM_OFFSET_V_MASK)) | ((a)<<CIM_OFFSET_V_BIT) )
#define __cim_set_h_offset(a) 	( REG_CIM_OFFSET = (REG_CIM_OFFSET&(~CIM_OFFSET_H_MASK)) | ((a)<<CIM_OFFSET_H_BIT) )
#define __cim_get_v_offset() 	((REG_CIM_OFFSET&CIM_OFFSET_V_MASK)>>CIM_OFFSET_V_BIT)
#define __cim_get_h_offset() 	((REG_CIM_OFFSET&CIM_OFFSET_H_MASK)>>CIM_OFFSET_H_BIT)

#endif /* __MIPS_ASSEMBLER */

/*
 * Clock reset and power controller module(CPM) address definition
 */
#define	CPM_BASE	0xb0000000

/*
 * CPM registers offset address definition
 */
#define CPM_CPCCR_OFFSET        (0x00)  /* rw, 32, 0x01011100 */
#define CPM_LCR_OFFSET          (0x04)  /* rw, 32, 0x000000f8 */
#define CPM_RSR_OFFSET          (0x08)  /* rw, 32, 0x???????? */
#define CPM_CPPCR0_OFFSET       (0x10)  /* rw, 32, 0x28080011 */
#define CPM_CPPSR_OFFSET        (0x14)  /* rw, 32, 0x80000000 */
#define CPM_CLKGR0_OFFSET       (0x20)  /* rw, 32, 0x3fffffe0 */
#define CPM_OPCR_OFFSET         (0x24)  /* rw, 32, 0x00001570 */
#define CPM_CLKGR1_OFFSET       (0x28)  /* rw, 32, 0x0000017f */
#define CPM_CPPCR1_OFFSET       (0x30)  /* rw, 32, 0x28080002 */
#define CPM_CPSPR_OFFSET        (0x34)  /* rw, 32, 0x???????? */
#define CPM_CPSPPR_OFFSET       (0x38)  /* rw, 32, 0x0000a5a5 */
#define CPM_USBPCR_OFFSET       (0x3c)  /* rw, 32, 0x42992198 */
#define CPM_USBRDT_OFFSET       (0x40)  /* rw, 32, 0x00000096 */
#define CPM_USBVBFIL_OFFSET     (0x44)  /* rw, 32, 0x00000080 */
#define CPM_USBCDR_OFFSET       (0x50)  /* rw, 32, 0x00000000 */
#define CPM_I2SCDR_OFFSET       (0x60)  /* rw, 32, 0x00000000 */
#define CPM_LPCDR_OFFSET        (0x64)  /* rw, 32, 0x00000000 */
#define CPM_MSCCDR_OFFSET       (0x68)  /* rw, 32, 0x00000000 */
#define CPM_UHCCDR_OFFSET       (0x6c)  /* rw, 32, 0x00000000 */
#define CPM_SSICDR_OFFSET       (0x74)  /* rw, 32, 0x00000000 */
#define CPM_CIMCDR_OFFSET       (0x7c)  /* rw, 32, 0x00000000 */
#define CPM_GPSCDR_OFFSET       (0x80)  /* rw, 32, 0x00000000 */
#define CPM_PCMCDR_OFFSET       (0x84)  /* rw, 32, 0x00000000 */
#define CPM_GPUCDR_OFFSET       (0x88)  /* rw, 32, 0x00000000 */
#define CPM_PSWC0ST_OFFSET      (0x90)  /* rw, 32, 0x00000000 */
#define CPM_PSWC1ST_OFFSET      (0x94)  /* rw, 32, 0x00000000 */
#define CPM_PSWC2ST_OFFSET      (0x98)  /* rw, 32, 0x00000000 */
#define CPM_PSWC3ST_OFFSET      (0x9c)  /* rw, 32, 0x00000000 */

/*
 * CPM registers address definition
 */
#define CPM_CPCCR        (CPM_BASE + CPM_CPCCR_OFFSET)
#define CPM_LCR          (CPM_BASE + CPM_LCR_OFFSET)
#define CPM_RSR          (CPM_BASE + CPM_RSR_OFFSET)
#define CPM_CPPCR0       (CPM_BASE + CPM_CPPCR0_OFFSET)
#define CPM_CPPSR        (CPM_BASE + CPM_CPPSR_OFFSET)
#define CPM_CLKGR0       (CPM_BASE + CPM_CLKGR0_OFFSET)
#define CPM_OPCR         (CPM_BASE + CPM_OPCR_OFFSET)
#define CPM_CLKGR1       (CPM_BASE + CPM_CLKGR1_OFFSET)
#define CPM_CPPCR1       (CPM_BASE + CPM_CPPCR1_OFFSET)
#define CPM_CPSPR        (CPM_BASE + CPM_CPSPR_OFFSET)
#define CPM_CPSPPR       (CPM_BASE + CPM_CPSPPR_OFFSET)
#define CPM_USBPCR       (CPM_BASE + CPM_USBPCR_OFFSET)
#define CPM_USBRDT       (CPM_BASE + CPM_USBRDT_OFFSET)
#define CPM_USBVBFIL     (CPM_BASE + CPM_USBVBFIL_OFFSET)
#define CPM_USBCDR       (CPM_BASE + CPM_USBCDR_OFFSET)
#define CPM_I2SCDR       (CPM_BASE + CPM_I2SCDR_OFFSET)
#define CPM_LPCDR        (CPM_BASE + CPM_LPCDR_OFFSET)
#define CPM_MSCCDR       (CPM_BASE + CPM_MSCCDR_OFFSET)
#define CPM_UHCCDR       (CPM_BASE + CPM_UHCCDR_OFFSET)
#define CPM_SSICDR       (CPM_BASE + CPM_SSICDR_OFFSET)
#define CPM_CIMCDR       (CPM_BASE + CPM_CIMCDR_OFFSET)
#define CPM_GPSCDR       (CPM_BASE + CPM_GPSCDR_OFFSET)
#define CPM_PCMCDR       (CPM_BASE + CPM_PCMCDR_OFFSET)
#define CPM_GPUCDR       (CPM_BASE + CPM_GPUCDR_OFFSET)
#define CPM_PSWC0ST      (CPM_BASE + CPM_PSWC0ST_OFFSET)
#define CPM_PSWC1ST      (CPM_BASE + CPM_PSWC1ST_OFFSET)
#define CPM_PSWC2ST      (CPM_BASE + CPM_PSWC2ST_OFFSET)
#define CPM_PSWC3ST      (CPM_BASE + CPM_PSWC3ST_OFFSET)

/*
 * CPM registers common define
 */

/* Clock control register(CPCCR) */
#define CPCCR_ECS               BIT31
#define CPCCR_MEM               BIT30
#define CPCCR_CE                BIT22
#define CPCCR_PCS               BIT21

#define CPCCR_SDIV_LSB          24
#define CPCCR_SDIV_MASK         BITS_H2L(27, CPCCR_SDIV_LSB)

#define CPCCR_H2DIV_LSB         16
#define CPCCR_H2DIV_MASK        BITS_H2L(19, CPCCR_H2DIV_LSB)

#define CPCCR_MDIV_LSB          12
#define CPCCR_MDIV_MASK         BITS_H2L(15, CPCCR_MDIV_LSB)

#define CPCCR_PDIV_LSB		8
#define CPCCR_PDIV_MASK         BITS_H2L(11, CPCCR_PDIV_LSB)

#define CPCCR_HDIV_LSB		4
#define CPCCR_HDIV_MASK         BITS_H2L(7, CPCCR_HDIV_LSB)

#define CPCCR_CDIV_LSB		0
#define CPCCR_CDIV_MASK         BITS_H2L(3, CPCCR_CDIV_LSB)

/* Low power control register(LCR) */
#define LCR_PDAHB1              BIT30
#define LCR_PDAHB1S             BIT26
#define LCR_DOZE                BIT2

#define LCR_PST_LSB             8
#define LCR_PST_MASK            BITS_H2L(19, LCR_PST_LSB)

#define LCR_DUTY_LSB            3
#define LCR_DUTY_MASK           BITS_H2L(7, LCR_DUTY_LSB)

#define LCR_LPM_LSB             0
#define LCR_LPM_MASK            BITS_H2L(1, LCR_LPM_LSB)
#define LCR_LPM_IDLE            (0x0 << LCR_LPM_LSB)
#define LCR_LPM_SLEEP           (0x1 << LCR_LPM_LSB)

/* Reset status register(RSR) */
#define RSR_P0R         BIT2
#define RSR_WR          BIT1
#define RSR_PR          BIT0

/* PLL control register 0(CPPCR0) */
#define CPPCR0_LOCK             BIT15   /* LOCK0 bit */
#define CPPCR0_PLLS             BIT10
#define CPPCR0_PLLBP            BIT9
#define CPPCR0_PLLEN            BIT8

#define CPPCR0_PLLM_LSB         24
#define CPPCR0_PLLM_MASK        BITS_H2L(30, CPPCR0_PLLM_LSB)

#define CPPCR0_PLLN_LSB         18
#define CPPCR0_PLLN_MASK        BITS_H2L(21, CPPCR0_PLLN_LSB)

#define CPPCR0_PLLOD_LSB        16
#define CPPCR0_PLLOD_MASK       BITS_H2L(17, CPPCR0_PLLOD_LSB)

#define CPPCR0_PLLST_LSB        0
#define CPPCR0_PLLST_MASK       BITS_H2L(7, CPPCR0_PLLST_LSB)

/* PLL switch and status register(CPPSR) */
#define CPPSR_PLLOFF            BIT31
#define CPPSR_PLLBP             BIT30
#define CPPSR_PLLON             BIT29
#define CPPSR_PS                BIT28
#define CPPSR_FS                BIT27
#define CPPSR_CS                BIT26
#define CPPSR_SM                BIT2
#define CPPSR_PM                BIT1
#define CPPSR_FM                BIT0

/* Clock gate register 0(CGR0) */
#define CLKGR0_AHB_MON		BIT31
#define CLKGR0_DDR              BIT30
#define CLKGR0_IPU              BIT29
#define CLKGR0_LCD              BIT28
#define CLKGR0_TVE              BIT27
#define CLKGR0_CIM              BIT26
#define CLKGR0_MDMA             BIT25
#define CLKGR0_UHC              BIT24
#define CLKGR0_MAC              BIT23
#define CLKGR0_GPS              BIT22
#define CLKGR0_DMAC             BIT21
#define CLKGR0_SSI2             BIT20
#define CLKGR0_SSI1             BIT19
#define CLKGR0_UART3            BIT18
#define CLKGR0_UART2            BIT17
#define CLKGR0_UART1            BIT16
#define CLKGR0_UART0            BIT15
#define CLKGR0_SADC             BIT14
#define CLKGR0_KBC              BIT13
#define CLKGR0_MSC2             BIT12
#define CLKGR0_MSC1             BIT11
#define CLKGR0_OWI              BIT10
#define CLKGR0_TSSI             BIT9
#define CLKGR0_AIC              BIT8
#define CLKGR0_SCC              BIT7
#define CLKGR0_I2C1             BIT6
#define CLKGR0_I2C0             BIT5
#define CLKGR0_SSI0             BIT4
#define CLKGR0_MSC0             BIT3
#define CLKGR0_OTG              BIT2
#define CLKGR0_BCH              BIT1
#define CLKGR0_NEMC             BIT0

/* Oscillator and power control register(OPCR) */
#define OPCR_OTGPHY_ENABLE      BIT7    /* SPENDN bit */
#define OPCR_GPSEN              BIT6
#define OPCR_UHCPHY_DISABLE     BIT5    /* SPENDH bit */
#define OPCR_O1SE               BIT4
#define OPCR_PD                 BIT3
#define OPCR_ERCS               BIT2

#define OPCR_O1ST_LSB           8
#define OPCR_O1ST_MASK          BITS_H2L(15, OPCR_O1ST_LSB)

/* Clock gate register 1(CGR1) */
#define CLKGR1_AUX		BIT11
#define CLKGR1_OSD		BIT10
#define CLKGR1_GPU              BIT9
#define CLKGR1_PCM              BIT8
#define CLKGR1_AHB1             BIT7
#define CLKGR1_CABAC            BIT6
#define CLKGR1_SRAM             BIT5
#define CLKGR1_DCT              BIT4
#define CLKGR1_ME               BIT3
#define CLKGR1_DBLK             BIT2
#define CLKGR1_MC               BIT1
#define CLKGR1_BDMA             BIT0

/* PLL control register 1(CPPCR1) */
#define CPPCR1_P1SCS            BIT15
#define CPPCR1_PLL1EN           BIT7
#define CPPCR1_PLL1S            BIT6
#define CPPCR1_LOCK             BIT2    /* LOCK1 bit */
#define CPPCR1_PLL1OFF          BIT1
#define CPPCR1_PLL1ON           BIT0

#define CPPCR1_PLL1M_LSB        24
#define CPPCR1_PLL1M_MASK       BITS_H2L(30, CPPCR1_PLL1M_LSB)

#define CPPCR1_PLL1N_LSB        18
#define CPPCR1_PLL1N_MASK       BITS_H2L(21, CPPCR1_PLL1N_LSB)

#define CPPCR1_PLL1OD_LSB       16
#define CPPCR1_PLL1OD_MASK      BITS_H2L(17, CPPCR1_PLL1OD_LSB)

#define CPPCR1_P1SDIV_LSB       9
#define CPPCR1_P1SDIV_MASK      BITS_H2L(14, CPPCR1_P1SDIV_LSB)

/* CPM scratch pad protected register(CPSPPR) */
#define CPSPPR_CPSPR_WRITABLE   (0x00005a5a)

/* OTG parameter control register(USBPCR) */
#define USBPCR_USB_MODE         BIT31
#define USBPCR_AVLD_REG         BIT30
#define USBPCR_INCRM            BIT27   /* INCR_MASK bit */
#define USBPCR_CLK12_EN         BIT26
#define USBPCR_COMMONONN        BIT25
#define USBPCR_VBUSVLDEXT       BIT24
#define USBPCR_VBUSVLDEXTSEL    BIT23
#define USBPCR_POR              BIT22
#define USBPCR_SIDDQ            BIT21
#define USBPCR_OTG_DISABLE      BIT20
#define USBPCR_TXPREEMPHTUNE    BIT6

#define USBPCR_IDPULLUP_LSB             28   /* IDPULLUP_MASK bit */
#define USBPCR_IDPULLUP_MASK            BITS_H2L(29, USBPCR_USBPCR_IDPULLUP_LSB)

#define USBPCR_COMPDISTUNE_LSB          17
#define USBPCR_COMPDISTUNE_MASK         BITS_H2L(19, USBPCR_COMPDISTUNE_LSB)

#define USBPCR_OTGTUNE_LSB              14
#define USBPCR_OTGTUNE_MASK             BITS_H2L(16, USBPCR_OTGTUNE_LSB)

#define USBPCR_SQRXTUNE_LSB             11
#define USBPCR_SQRXTUNE_MASK            BITS_H2L(13, USBPCR_SQRXTUNE_LSB)

#define USBPCR_TXFSLSTUNE_LSB           7
#define USBPCR_TXFSLSTUNE_MASK          BITS_H2L(10, USBPCR_TXFSLSTUNE_LSB)

#define USBPCR_TXRISETUNE_LSB           4
#define USBPCR_TXRISETUNE_MASK          BITS_H2L(5, USBPCR_TXRISETUNE_LSB)

#define USBPCR_TXVREFTUNE_LSB           0
#define USBPCR_TXVREFTUNE_MASK          BITS_H2L(3, USBPCR_TXVREFTUNE_LSB)

/* OTG reset detect timer register(USBRDT) */
#define USBRDT_HB_MASK		BIT26
#define USBRDT_VBFIL_LD_EN      BIT25
#define USBRDT_IDDIG_EN         BIT24
#define USBRDT_IDDIG_REG        BIT23

#define USBRDT_USBRDT_LSB       0
#define USBRDT_USBRDT_MASK      BITS_H2L(22, USBRDT_USBRDT_LSB)

/* OTG PHY clock divider register(USBCDR) */
#define USBCDR_UCS              BIT31
#define USBCDR_UPCS             BIT30

#define USBCDR_OTGDIV_LSB       0       /* USBCDR bit */
#define USBCDR_OTGDIV_MASK      BITS_H2L(5, USBCDR_OTGDIV_LSB)

/* I2S device clock divider register(I2SCDR) */
#define I2SCDR_I2CS             BIT31
#define I2SCDR_I2PCS            BIT30

#define I2SCDR_I2SDIV_LSB       0       /* I2SCDR bit */
#define I2SCDR_I2SDIV_MASK      BITS_H2L(8, I2SCDR_I2SDIV_LSB)

/* LCD pix clock divider register(LPCDR) */
//#define LPCDR_LSCS              BIT31
#define LPCDR_LTCS              BIT30
#define LPCDR_LPCS              BIT29

#define LPCDR_PIXDIV_LSB        0       /* LPCDR bit */
#define LPCDR_PIXDIV_MASK       BITS_H2L(10, LPCDR_PIXDIV_LSB)

/* MSC clock divider register(MSCCDR) */
#define MSCCDR_MCS              BIT31

#define MSCCDR_MSCDIV_LSB       0       /* MSCCDR bit */
#define MSCCDR_MSCDIV_MASK      BITS_H2L(4, MSCCDR_MSCDIV_LSB)

/* UHC device clock divider register(UHCCDR) */
#define UHCCDR_UHPCS            BIT31

#define UHCCDR_UHCDIV_LSB       0       /* UHCCDR bit */
#define UHCCDR_UHCDIV_MASK      BITS_H2L(3, UHCCDR_UHCDIV_LSB)

/* SSI clock divider register(SSICDR) */
#define SSICDR_SCS              BIT31

#define SSICDR_SSIDIV_LSB       0       /* SSICDR bit */
#define SSICDR_SSIDIV_MASK      BITS_H2L(3, SSICDR_SSIDIV_LSB)

/* CIM mclk clock divider register(CIMCDR) */
#define CIMCDR_CIMDIV_LSB       0       /* CIMCDR bit */
#define CIMCDR_CIMDIV_MASK      BITS_H2L(7, CIMCDR_CIMDIV_LSB)

/* GPS clock divider register(GPSCDR) */
#define GPSCDR_GPCS             BIT31

#define GPSCDR_GPSDIV_LSB       0       /* GPSCDR bit */
#define GSPCDR_GPSDIV_MASK      BITS_H2L(3, GPSCDR_GPSDIV_LSB)

/* PCM device clock divider register(PCMCDR) */
#define PCMCDR_PCMS             BIT31
#define PCMCDR_PCMPCS           BIT30

#define PCMCDR_PCMDIV_LSB       0       /* PCMCDR bit */
#define PCMCDR_PCMDIV_MASK      BITS_H2L(8, PCMCDR_PCMDIV_LSB)

/* GPU clock divider register */
#define GPUCDR_GPCS             BIT31
#define GPUCDR_GPUDIV_LSB       0       /* GPUCDR bit */
#define GPUCDR_GPUDIV_MASK      BITS_H2L(2, GPUCDR_GPUDIV_LSB)

#ifndef __MIPS_ASSEMBLER

#define REG_CPM_CPCCR           REG32(CPM_CPCCR)
#define REG_CPM_RSR             REG32(CPM_RSR)
#define REG_CPM_CPPCR0          REG32(CPM_CPPCR0)
#define REG_CPM_CPPSR           REG32(CPM_CPPSR)
#define REG_CPM_CPPCR1          REG32(CPM_CPPCR1)
#define REG_CPM_CPSPR           REG32(CPM_CPSPR)
#define REG_CPM_CPSPPR          REG32(CPM_CPSPPR)
#define REG_CPM_USBPCR          REG32(CPM_USBPCR)
#define REG_CPM_USBRDT          REG32(CPM_USBRDT)
#define REG_CPM_USBVBFIL        REG32(CPM_USBVBFIL)
#define REG_CPM_USBCDR          REG32(CPM_USBCDR)
#define REG_CPM_I2SCDR          REG32(CPM_I2SCDR)
#define REG_CPM_LPCDR           REG32(CPM_LPCDR)
#define REG_CPM_MSCCDR          REG32(CPM_MSCCDR)
#define REG_CPM_UHCCDR          REG32(CPM_UHCCDR)
#define REG_CPM_SSICDR          REG32(CPM_SSICDR)
#define REG_CPM_CIMCDR          REG32(CPM_CIMCDR)
#define REG_CPM_GPSCDR          REG32(CPM_GPSCDR)
#define REG_CPM_PCMCDR          REG32(CPM_PCMCDR)
#define REG_CPM_GPUCDR          REG32(CPM_GPUCDR)

#define REG_CPM_PSWC0ST         REG32(CPM_PSWC0ST)
#define REG_CPM_PSWC1ST         REG32(CPM_PSWC1ST)
#define REG_CPM_PSWC2ST         REG32(CPM_PSWC2ST)
#define REG_CPM_PSWC3ST         REG32(CPM_PSWC3ST)

#define REG_CPM_LCR             REG32(CPM_LCR)
#define REG_CPM_CLKGR0          REG32(CPM_CLKGR0)
#define REG_CPM_OPCR            REG32(CPM_OPCR)
#define REG_CPM_CLKGR1          REG32(CPM_CLKGR1)
#define REG_CPM_CLKGR           REG32(CPM_CLKGR0)

#define cpm_get_scrpad()	INREG32(CPM_CPSPR)
#define cpm_set_scrpad(data)				\
do {							\
	OUTREG32(CPM_CPSPPR, CPSPPR_CPSPR_WRITABLE);	\
	OUTREG32(CPM_CPSPR, data);			\
	OUTREG32(CPM_CPSPPR, ~CPSPPR_CPSPR_WRITABLE);	\
} while (0)

#define CPM_POWER_ON    1
#define CPM_POWER_OFF   0

/***************************************************************************
 * CPM									   *
 ***************************************************************************/
#define __cpm_get_pllm() \
	((REG_CPM_CPPCR0 & CPPCR0_PLLM_MASK) >> CPPCR0_PLLM_LSB)
#define __cpm_get_plln() \
	((REG_CPM_CPPCR0 & CPPCR0_PLLN_MASK) >> CPPCR0_PLLN_LSB)
#define __cpm_get_pllod() \
	((REG_CPM_CPPCR0 & CPPCR0_PLLOD_MASK) >> CPPCR0_PLLOD_LSB)

#define __cpm_get_pll1m() \
	((REG_CPM_CPPCR1 & CPPCR1_PLL1M_MASK) >> CPPCR1_PLL1M_LSB)
#define __cpm_get_pll1n() \
	((REG_CPM_CPPCR1 & CPPCR1_PLL1N_MASK) >> CPPCR1_PLL1N_LSB)
#define __cpm_get_pll1od() \
	((REG_CPM_CPPCR1 & CPPCR1_PLL1OD_MASK) >> CPPCR1_PLL1OD_LSB)

#define __cpm_get_cdiv() \
	((REG_CPM_CPCCR & CPCCR_CDIV_MASK) >> CPCCR_CDIV_LSB)
#define __cpm_get_hdiv() \
	((REG_CPM_CPCCR & CPCCR_HDIV_MASK) >> CPCCR_HDIV_LSB)
#define __cpm_get_h2div() \
	((REG_CPM_CPCCR & CPCCR_H2DIV_MASK) >> CPCCR_H2DIV_LSB)
#define __cpm_get_pdiv() \
	((REG_CPM_CPCCR & CPCCR_PDIV_MASK) >> CPCCR_PDIV_LSB)
#define __cpm_get_mdiv() \
	((REG_CPM_CPCCR & CPCCR_MDIV_MASK) >> CPCCR_MDIV_LSB)
#define __cpm_get_sdiv() \
	((REG_CPM_CPCCR & CPCCR_SDIV_MASK) >> CPCCR_SDIV_LSB)
#define __cpm_get_i2sdiv() \
	((REG_CPM_I2SCDR & I2SCDR_I2SDIV_MASK) >> I2SCDR_I2SDIV_LSB)
#define __cpm_get_pixdiv() \
	((REG_CPM_LPCDR & LPCDR_PIXDIV_MASK) >> LPCDR_PIXDIV_LSB)
#define __cpm_get_mscdiv() \
	((REG_CPM_MSCCDR & MSCCDR_MSCDIV_MASK) >> MSCCDR_MSCDIV_LSB)
#define __cpm_get_ssidiv() \
	((REG_CPM_SSICCDR & SSICDR_SSICDIV_MASK) >> SSICDR_SSIDIV_LSB)
#define __cpm_get_pcmdiv() \
	((REG_CPM_PCMCDR & PCMCDR_PCMCD_MASK) >> PCMCDR_PCMCD_LSB)
#define __cpm_get_pll1div() \
	((REG_CPM_CPPCR1 & CPCCR1_P1SDIV_MASK) >> CPCCR1_P1SDIV_LSB)

#define __cpm_set_cdiv(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_CDIV_MASK) | ((v) << (CPCCR_CDIV_LSB)))
#define __cpm_set_hdiv(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_HDIV_MASK) | ((v) << (CPCCR_HDIV_LSB)))
#define __cpm_set_pdiv(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_PDIV_MASK) | ((v) << (CPCCR_PDIV_LSB)))
#define __cpm_set_mdiv(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_MDIV_MASK) | ((v) << (CPCCR_MDIV_LSB)))
#define __cpm_set_h1div(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_H1DIV_MASK) | ((v) << (CPCCR_H1DIV_LSB)))
#define __cpm_set_udiv(v) \
	(REG_CPM_CPCCR = (REG_CPM_CPCCR & ~CPCCR_UDIV_MASK) | ((v) << (CPCCR_UDIV_LSB)))
#define __cpm_set_i2sdiv(v) \
	(REG_CPM_I2SCDR = (REG_CPM_I2SCDR & ~I2SCDR_I2SDIV_MASK) | ((v) << (I2SCDR_I2SDIV_LSB)))
#define __cpm_set_pixdiv(v) \
	(REG_CPM_LPCDR = (REG_CPM_LPCDR & ~LPCDR_PIXDIV_MASK) | ((v) << (LPCDR_PIXDIV_LSB)))
#define __cpm_set_mscdiv(v) \
	(REG_CPM_MSCCDR = (REG_CPM_MSCCDR & ~MSCCDR_MSCDIV_MASK) | ((v) << (MSCCDR_MSCDIV_LSB)))
#define __cpm_set_ssidiv(v) \
	(REG_CPM_SSICDR = (REG_CPM_SSICDR & ~SSICDR_SSIDIV_MASK) | ((v) << (SSICDR_SSIDIV_LSB)))
#define __cpm_set_pcmdiv(v) \
	(REG_CPM_PCMCDR = (REG_CPM_PCMCDR & ~PCMCDR_PCMCD_MASK) | ((v) << (PCMCDR_PCMCD_LSB)))
#define __cpm_set_pll1div(v) \
	(REG_CPM_CPPCR1 = (REG_CPM_CPPCR1 & ~CPCCR1_P1SDIV_MASK) | ((v) << (CPCCR1_P1SDIV_LSB)))

#define __cpm_select_i2sclk_pll1() 	(REG_CPM_I2SCDR |= I2SCDR_I2PCS)
#define __cpm_select_i2sclk_pll0()	(REG_CPM_I2SCDR &= ~I2SCDR_I2PCS)
#define __cpm_select_otgclk_pll1() 	(REG_CPM_USBCDR |= USBCDR_UPCS)
#define __cpm_select_otgclk_pll0()	(REG_CPM_USBCDR &= ~USBCDR_UPCS)
#define __cpm_select_lcdpclk_pll1() 	(REG_CPM_LPCDR |= LPCDR_LPCS)
#define __cpm_select_lcdpclk_pll0()	(REG_CPM_LPCDR &= ~LPCDR_LPCS)
#define __cpm_select_uhcclk_pll1() 	(REG_CPM_UHCCDR |= UHCCDR_UHPCS)
#define __cpm_select_uhcclk_pll0()	(REG_CPM_UHCCDR &= ~UHCCDR_UHPCS)
#define __cpm_select_gpsclk_pll1() 	(REG_CPM_GPSCDR |= GPSCDR_GPCS)
#define __cpm_select_gpsclk_pll0()	(REG_CPM_GPSCDR &= ~GPSCDR_GPCS)
#define __cpm_select_pcmclk_pll1() 	(REG_CPM_PCMCDR |= PCMCDR_PCMPCS)
#define __cpm_select_pcmclk_pll0()	(REG_CPM_PCMCDR &= ~PCMCDR_PCMPCS)
#define __cpm_select_gpuclk_pll1() 	(REG_CPM_GPUCDR |= GPUCDR_GPCS)
#define __cpm_select_gpuclk_pll0()	(REG_CPM_GPUCDR &= ~GPUCDR_GPCS)
#define __cpm_select_clk_pll1() 	(REG_CPM_CDR |= CDR_PCS)
#define __cpm_select_clk_pll0()		(REG_CPM_CDR &= ~CDR_PCS)


#define __cpm_select_pcmclk_pll() 	(REG_CPM_PCMCDR |= PCMCDR_PCMS)
#define __cpm_select_pcmclk_exclk() 	(REG_CPM_PCMCDR &= ~PCMCDR_PCMS)
#define __cpm_select_pixclk_ext()	(REG_CPM_LPCDR |= LPCDR_LPCS)
#define __cpm_select_pixclk_pll()	(REG_CPM_LPCDR &= ~LPCDR_LPCS)
#define __cpm_select_tveclk_exclk()	(REG_CPM_LPCDR |= CPCCR_LSCS)
#define __cpm_select_tveclk_pll()	(REG_CPM_LPCDR &= ~LPCDR_LSCS)
#define __cpm_select_pixclk_lcd()	(REG_CPM_LPCDR &= ~LPCDR_LTCS)
#define __cpm_select_pixclk_tve()	(REG_CPM_LPCDR |= LPCDR_LTCS)
#define __cpm_select_i2sclk_exclk()	(REG_CPM_I2SCDR &= ~I2SCDR_I2CS)
#define __cpm_select_i2sclk_pll()	(REG_CPM_I2SCDR |= I2SCDR_I2CS)
//#define __cpm_select_usbclk_exclk()	(REG_CPM_CPCCR &= ~CPCCR_UCS)
//#define __cpm_select_usbclk_pll()	(REG_CPM_CPCCR |= CPCCR_UCS)

#define __cpm_enable_cko()
#define __cpm_exclk_direct()		(REG_CPM_CPCCR &= ~CPCCR_ECS)
#define __cpm_exclk_div2()             	(REG_CPM_CPCCR |= CPCCR_ECS)
#define __cpm_enable_pll_change()	(REG_CPM_CPCCR |= CPCCR_CE)

#define __cpm_pllout_div2()		(REG_CPM_CPCCR &= ~CPCCR_PCS)
#define __cpm_pll_enable()		(REG_CPM_CPPCR0 |= CPPCR0_PLLEN)

#define __cpm_pll1_enable()		(REG_CPM_CPPCR1 |= CPPCR1_PLL1EN)

#define __cpm_pll_is_off()		(REG_CPM_CPPSR & CPPSR_PLLOFF)
#define __cpm_pll_is_on()		(REG_CPM_CPPSR & CPPSR_PLLON)
#define __cpm_pll_bypass()		(REG_CPM_CPPSR |= CPPSR_PLLBP)

#define __cpm_get_cclk_doze_duty() \
	((REG_CPM_LCR & LCR_DOZE_DUTY_MASK) >> LCR_DOZE_DUTY_LSB)
#define __cpm_set_cclk_doze_duty(v) \
	(REG_CPM_LCR = (REG_CPM_LCR & ~LCR_DOZE_DUTY_MASK) | ((v) << (LCR_DOZE_DUTY_LSB)))

#define __cpm_doze_mode()		(REG_CPM_LCR |= LCR_DOZE_ON)
#define __cpm_idle_mode() \
	(REG_CPM_LCR = (REG_CPM_LCR & ~LCR_LPM_MASK) | LCR_LPM_IDLE)
#define __cpm_sleep_mode() \
	(REG_CPM_LCR = (REG_CPM_LCR & ~LCR_LPM_MASK) | LCR_LPM_SLEEP)

#define __cpm_stop_all() 	\
	do {\
		(REG_CPM_CLKGR0 = 0xffffffff);\
		(REG_CPM_CLKGR1 = 0x3ff);\
	}while(0)
#define __cpm_stop_emc()	(REG_CPM_CLKGR0 |= CLKGR0_EMC)
#define __cpm_stop_ddr()	(REG_CPM_CLKGR0 |= CLKGR0_DDR)
#define __cpm_stop_ipu()	(REG_CPM_CLKGR0 |= CLKGR0_IPU)
#define __cpm_stop_lcd()	(REG_CPM_CLKGR0 |= CLKGR0_LCD)
#define __cpm_stop_tve()	(REG_CPM_CLKGR0 |= CLKGR0_TVE)
#define __cpm_stop_Cim()	(REG_CPM_CLKGR0 |= CLKGR0_CIM)
#define __cpm_stop_mdma()	(REG_CPM_CLKGR0 |= CLKGR0_MDMA)
#define __cpm_stop_uhc()	(REG_CPM_CLKGR0 |= CLKGR0_UHC)
#define __cpm_stop_mac()	(REG_CPM_CLKGR0 |= CLKGR0_MAC)
#define __cpm_stop_gps()	(REG_CPM_CLKGR0 |= CLKGR0_GPS)
#define __cpm_stop_dmac()	(REG_CPM_CLKGR0 |= CLKGR0_DMAC)
#define __cpm_stop_ssi2()	(REG_CPM_CLKGR0 |= CLKGR0_SSI2)
#define __cpm_stop_ssi1()	(REG_CPM_CLKGR0 |= CLKGR0_SSI1)
#define __cpm_stop_uart3()	(REG_CPM_CLKGR0 |= CLKGR0_UART3)
#define __cpm_stop_uart2()	(REG_CPM_CLKGR0 |= CLKGR0_UART2)
#define __cpm_stop_uart1()	(REG_CPM_CLKGR0 |= CLKGR0_UART1)
#define __cpm_stop_uart0()	(REG_CPM_CLKGR0 |= CLKGR0_UART0)
#define __cpm_stop_sadc()	(REG_CPM_CLKGR0 |= CLKGR0_SADC)
#define __cpm_stop_kbc()	(REG_CPM_CLKGR0 |= CLKGR0_KBC)
#define __cpm_stop_msc2()	(REG_CPM_CLKGR0 |= CLKGR0_MSC2)
#define __cpm_stop_msc1()	(REG_CPM_CLKGR0 |= CLKGR0_MSC1)
#define __cpm_stop_owi()	(REG_CPM_CLKGR0 |= CLKGR0_OWI)
#define __cpm_stop_tssi()	(REG_CPM_CLKGR0 |= CLKGR0_TSSI)
#define __cpm_stop_aic()	(REG_CPM_CLKGR0 |= CLKGR0_AIC)
#define __cpm_stop_scc()	(REG_CPM_CLKGR0 |= CLKGR0_SCC)
#define __cpm_stop_i2c1()	(REG_CPM_CLKGR0 |= CLKGR0_I2C1)
#define __cpm_stop_i2c0()	(REG_CPM_CLKGR0 |= CLKGR0_I2C0)
#define __cpm_stop_ssi0()	(REG_CPM_CLKGR0 |= CLKGR0_SSI0)
#define __cpm_stop_msc0()	(REG_CPM_CLKGR0 |= CLKGR0_MSC0)
#define __cpm_stop_otg()	(REG_CPM_CLKGR0 |= CLKGR0_OTG)
#define __cpm_stop_bch()	(REG_CPM_CLKGR0 |= CLKGR0_BCH)
#define __cpm_stop_nemc()	(REG_CPM_CLKGR0 |= CLKGR0_NEMC)
#define __cpm_stop_gpu()	(REG_CPM_CLKGR1 |= CLKGR1_GPU)
#define __cpm_stop_pcm()	(REG_CPM_CLKGR1 |= CLKGR1_PCM)
#define __cpm_stop_ahb1()	(REG_CPM_CLKGR1 |= CLKGR1_AHB1)
#define __cpm_stop_cabac()	(REG_CPM_CLKGR1 |= CLKGR1_CABAC)
#define __cpm_stop_sram()	(REG_CPM_CLKGR1 |= CLKGR1_SRAM)
#define __cpm_stop_dct()	(REG_CPM_CLKGR1 |= CLKGR1_DCT)
#define __cpm_stop_me()		(REG_CPM_CLKGR1 |= CLKGR1_ME)
#define __cpm_stop_dblk()	(REG_CPM_CLKGR1 |= CLKGR1_DBLK)
#define __cpm_stop_mc()		(REG_CPM_CLKGR1 |= CLKGR1_MC)
#define __cpm_stop_bdma()	(REG_CPM_CLKGR1 |= CLKGR1_BDMA)

#define __cpm_start_all() 	\
	do {\
		REG_CPM_CLKGR0 = 0x0;\
		REG_CPM_CLKGR1 = 0x0;\
	} while(0)
#define __cpm_start_emc()	(REG_CPM_CLKGR0 &= ~CLKGR0_EMC)
#define __cpm_start_ddr()	(REG_CPM_CLKGR0 &= ~CLKGR0_DDR)
#define __cpm_start_ipu()	(REG_CPM_CLKGR0 &= ~CLKGR0_IPU)
#define __cpm_start_lcd()	(REG_CPM_CLKGR0 &= ~CLKGR0_LCD)
#define __cpm_start_tve()	(REG_CPM_CLKGR0 &= ~CLKGR0_TVE)
#define __cpm_start_Cim()	(REG_CPM_CLKGR0 &= ~CLKGR0_CIM)
#define __cpm_start_mdma()	(REG_CPM_CLKGR0 &= ~CLKGR0_MDMA)
#define __cpm_start_uhc()	(REG_CPM_CLKGR0 &= ~CLKGR0_UHC)
#define __cpm_start_mac()	(REG_CPM_CLKGR0 &= ~CLKGR0_MAC)
#define __cpm_start_gps()	(REG_CPM_CLKGR0 &= ~CLKGR0_GPS)
#define __cpm_start_dmac()	(REG_CPM_CLKGR0 &= ~CLKGR0_DMAC)
#define __cpm_start_ssi2()	(REG_CPM_CLKGR0 &= ~CLKGR0_SSI2)
#define __cpm_start_ssi1()	(REG_CPM_CLKGR0 &= ~CLKGR0_SSI1)
#define __cpm_start_uart3()	(REG_CPM_CLKGR0 &= ~CLKGR0_UART3)
#define __cpm_start_uart2()	(REG_CPM_CLKGR0 &= ~CLKGR0_UART2)
#define __cpm_start_uart1()	(REG_CPM_CLKGR0 &= ~CLKGR0_UART1)
#define __cpm_start_uart0()	(REG_CPM_CLKGR0 &= ~CLKGR0_UART0)
#define __cpm_start_sadc()	(REG_CPM_CLKGR0 &= ~CLKGR0_SADC)
#define __cpm_start_kbc()	(REG_CPM_CLKGR0 &= ~CLKGR0_KBC)
#define __cpm_start_msc2()	(REG_CPM_CLKGR0 &= ~CLKGR0_MSC2)
#define __cpm_start_msc1()	(REG_CPM_CLKGR0 &= ~CLKGR0_MSC1)
#define __cpm_start_owi()	(REG_CPM_CLKGR0 &= ~CLKGR0_OWI)
#define __cpm_start_tssi()	(REG_CPM_CLKGR0 &= ~CLKGR0_TSSI)
#define __cpm_start_aic()	(REG_CPM_CLKGR0 &= ~CLKGR0_AIC)
#define __cpm_start_scc()	(REG_CPM_CLKGR0 &= ~CLKGR0_SCC)
#define __cpm_start_i2c1()	(REG_CPM_CLKGR0 &= ~CLKGR0_I2C1)
#define __cpm_start_i2c0()	(REG_CPM_CLKGR0 &= ~CLKGR0_I2C0)
#define __cpm_start_ssi0()	(REG_CPM_CLKGR0 &= ~CLKGR0_SSI0)
#define __cpm_start_msc0()	(REG_CPM_CLKGR0 &= ~CLKGR0_MSC0)
#define __cpm_start_otg()	(REG_CPM_CLKGR0 &= ~CLKGR0_OTG)
#define __cpm_start_bch()	(REG_CPM_CLKGR0 &= ~CLKGR0_BCH)
#define __cpm_start_nemc()	(REG_CPM_CLKGR0 &= ~CLKGR0_NEMC)
#define __cpm_start_gpu()	(REG_CPM_CLKGR1 &= ~CLKGR1_GPU)
#define __cpm_start_pcm()	(REG_CPM_CLKGR1 &= ~CLKGR1_PCM)
#define __cpm_start_ahb1()	(REG_CPM_CLKGR1 &= ~CLKGR1_AHB1)
#define __cpm_start_cabac()	(REG_CPM_CLKGR1 &= ~CLKGR1_CABAC)
#define __cpm_start_sram()	(REG_CPM_CLKGR1 &= ~CLKGR1_SRAM)
#define __cpm_start_dct()	(REG_CPM_CLKGR1 &= ~CLKGR1_DCT)
#define __cpm_start_me()	(REG_CPM_CLKGR1 &= ~CLKGR1_ME)
#define __cpm_start_dblk()	(REG_CPM_CLKGR1 &= ~CLKGR1_DBLK)
#define __cpm_start_mc()	(REG_CPM_CLKGR1 &= ~CLKGR1_MC)
#define __cpm_start_bdma()	(REG_CPM_CLKGR1 &= ~CLKGR1_BDMA)

#define __cpm_get_o1st() \
	((REG_CPM_OPCR & OPCR_O1ST_MASK) >> OPCR_O1ST_LSB)
#define __cpm_set_o1st(v) \
	(REG_CPM_OPCR = (REG_CPM_OPCR & ~OPCR_O1ST_MASK) | ((v) << (OPCR_O1ST_LSB)))
#define __cpm_suspend_otgphy()		(REG_CPM_OPCR &= ~OPCR_OTGPHY_ENABLE)
#define __cpm_resume_otgphy()		(REG_CPM_OPCR |= OPCR_OTGPHY_ENABLE)
#define __cpm_enable_osc_in_sleep()	(REG_CPM_OPCR |= OPCR_OSC_ENABLE)
#define __cpm_select_rtcclk_rtc()	(REG_CPM_OPCR |= OPCR_ERCS)
#define __cpm_select_rtcclk_exclk()	(REG_CPM_OPCR &= ~OPCR_ERCS)

#ifdef CFG_EXTAL
#define JZ_EXTAL		CFG_EXTAL
#else
#define JZ_EXTAL		12000000
#endif
#define JZ_EXTAL2		32768 /* RTC clock */

/* PLL output frequency */
static __inline__ unsigned int __cpm_get_pllout(void)
{
	unsigned long m, n, no, pllout;
	unsigned long cppcr = REG_CPM_CPPCR0;
	unsigned long od[4] = {1, 2, 4, 8};
	if ((cppcr & CPPCR0_PLLEN) && (!(cppcr & CPPCR0_PLLBP))) {
		m = __cpm_get_pllm() * 2;
		n = __cpm_get_plln();
		no = od[__cpm_get_pllod()];
		pllout = ((JZ_EXTAL) * m / (n * no));
	} else
		pllout = JZ_EXTAL;
	return pllout;
}

/* PLL output frequency */
static __inline__ unsigned int __cpm_get_pll1out(void)
{
	unsigned long m, n, no, pllout;
	unsigned long cppcr1 = REG_CPM_CPPCR1;
	unsigned long od[4] = {1, 2, 4, 8};
	if (cppcr1 & CPPCR1_PLL1EN)
	{
		m = __cpm_get_pll1m() * 2;
		n = __cpm_get_pll1n();
		no = od[__cpm_get_pll1od()];
		if (cppcr1 & CPPCR1_P1SCS)
			pllout = ((__cpm_get_pllout()) * m / (n * no));
		else
			pllout = ((JZ_EXTAL) * m / (n * no));

	} else
		pllout = JZ_EXTAL;
	return pllout;
}

/* PLL output frequency for MSC/I2S/LCD/USB */
static __inline__ unsigned int __cpm_get_pllout2(void)
{
	if (REG_CPM_CPCCR & CPCCR_PCS)
		return __cpm_get_pllout();
	else
		return __cpm_get_pllout()/2;
}

/* CPU core clock */
static __inline__ unsigned int __cpm_get_cclk(void)
{
	int div[] = {1, 2, 3, 4, 6, 8};

	return __cpm_get_pllout() / div[__cpm_get_cdiv()];
}

/* AHB system bus clock */
static __inline__ unsigned int __cpm_get_hclk(void)
{
	int div[] = {1, 2, 3, 4, 6, 8};

	return __cpm_get_pllout() / div[__cpm_get_hdiv()];
}

/* Memory bus clock */
static __inline__ unsigned int __cpm_get_mclk(void)
{
	int div[] = {1, 2, 3, 4, 6, 8};

	return __cpm_get_pllout() / div[__cpm_get_mdiv()];
}

/* APB peripheral bus clock */
static __inline__ unsigned int __cpm_get_pclk(void)
{
	int div[] = {1, 2, 3, 4, 6, 8};

	return __cpm_get_pllout() / div[__cpm_get_pdiv()];
}

/* AHB1 module clock */
static __inline__ unsigned int __cpm_get_h2clk(void)
{
	int div[] = {1, 2, 3, 4, 6, 8};

	return __cpm_get_pllout() / div[__cpm_get_h2div()];
}

/* LCD pixel clock */
static __inline__ unsigned int __cpm_get_pixclk(void)
{
	return __cpm_get_pllout2() / (__cpm_get_pixdiv() + 1);
}

/* I2S clock */
static __inline__ unsigned int __cpm_get_i2sclk(void)
{
	if (REG_CPM_I2SCDR & I2SCDR_I2CS) {
		return __cpm_get_pllout2() / (__cpm_get_i2sdiv() + 1);
	}
	else {
		return JZ_EXTAL;
	}
}

/* USB clock */
/*
static __inline__ unsigned int __cpm_get_usbclk(void)
{
	if (REG_CPM_CPCCR & CPCCR_UCS) {
		return __cpm_get_pllout2() / (__cpm_get_udiv() + 1);
	}
	else {
		return JZ_EXTAL;
	}
}
*/
/* EXTAL clock for UART,I2C,SSI,TCU,USB-PHY */
static __inline__ unsigned int __cpm_get_extalclk(void)
{
	return JZ_EXTAL;
}

/* RTC clock for CPM,INTC,RTC,TCU,WDT */
static __inline__ unsigned int __cpm_get_rtcclk(void)
{
	return JZ_EXTAL2;
}

/*
 * Output 24MHz for SD and 16MHz for MMC.
 */
static inline void __cpm_select_msc_clk(int sd)
{
	unsigned int pllout2 = __cpm_get_pllout2();
	unsigned int div = 0;

	if (sd) {
		div = pllout2 / 24000000;
	}
	else {
		div = pllout2 / 16000000;
	}

	REG_CPM_MSCCDR = (div - 1)|(1<<31);
	REG_CPM_CPCCR |= CPCCR_CE;
}

/* MSC clock */
static __inline__ unsigned int __cpm_get_mscclk(void)
{
    return __cpm_get_pllout2() / (__cpm_get_mscdiv() + 1);
}

#endif /* __MIPS_ASSEMBLER */

#define	DDRC_BASE	0xB3020000

/*************************************************************************
 * DDRC (DDR Controller)
 *************************************************************************/
#define DDRC_ST			(DDRC_BASE + 0x0) /* DDR Status Register */
#define DDRC_CFG		(DDRC_BASE + 0x4) /* DDR Configure Register */
#define DDRC_CTRL		(DDRC_BASE + 0x8) /* DDR Control Register */
#define DDRC_LMR		(DDRC_BASE + 0xc) /* DDR Load-Mode-Register */
#define DDRC_TIMING1		(DDRC_BASE + 0x10) /* DDR Timing Config Register 1 */
#define DDRC_TIMING2		(DDRC_BASE + 0x14) /* DDR Timing Config Register 2 */
#define DDRC_REFCNT		(DDRC_BASE + 0x18) /* DDR  Auto-Refresh Counter */
#define DDRC_DQS		(DDRC_BASE + 0x1c) /* DDR DQS Delay Control Register */
#define DDRC_DQS_ADJ		(DDRC_BASE + 0x20) /* DDR DQS Delay Adjust Register */
#define DDRC_MMAP0		(DDRC_BASE + 0x24) /* DDR Memory Map Config Register */
#define DDRC_MMAP1		(DDRC_BASE + 0x28) /* DDR Memory Map Config Register */
#define DDRC_DDELAYCTRL1	(DDRC_BASE + 0x2c)
#define DDRC_DDELAYCTRL2	(DDRC_BASE + 0x30)
#define DDRC_DSTRB		(DDRC_BASE + 0x34)
#define DDRC_PMEMBS0		(DDRC_BASE + 0x50)
#define DDRC_PMEMBS1		(DDRC_BASE + 0x54)
#define DDRC_PMEMOSEL		(DDRC_BASE + 0x58)
#define DDRC_PMEMOEN		(DDRC_BASE + 0x5c)

/* DDRC Register */
#define REG_DDRC_ST		REG32(DDRC_ST)
#define REG_DDRC_CFG		REG32(DDRC_CFG)
#define REG_DDRC_CTRL		REG32(DDRC_CTRL)
#define REG_DDRC_LMR		REG32(DDRC_LMR)
#define REG_DDRC_TIMING1	REG32(DDRC_TIMING1)
#define REG_DDRC_TIMING2	REG32(DDRC_TIMING2)
#define REG_DDRC_REFCNT		REG32(DDRC_REFCNT)
#define REG_DDRC_DQS		REG32(DDRC_DQS)
#define REG_DDRC_DQS_ADJ	REG32(DDRC_DQS_ADJ)
#define REG_DDRC_MMAP0		REG32(DDRC_MMAP0)
#define REG_DDRC_MMAP1		REG32(DDRC_MMAP1)
#define REG_DDRC_DDELAYCTRL1	REG32(DDRC_DDELAYCTRL1)
#define REG_DDRC_DDELAYCTRL2	REG32(DDRC_DDELAYCTRL2)
#define REG_DDRC_DSTRB		REG32(DDRC_DSTRB)
#define REG_DDRC_PMEMBS0	REG32(DDRC_PMEMBS0)
#define REG_DDRC_PMEMBS1	REG32(DDRC_PMEMBS1)
#define REG_DDRC_PMEMOSEL	REG32(DDRC_PMEMOSEL)
#define REG_DDRC_PMEMOEN	REG32(DDRC_PMEMOEN)

/* DDRC Status Register */
#define DDRC_ST_ENDIAN		(1 << 7) /* 0 Little data endian
					    1 Big data endian */
#define DDRC_ST_MISS		(1 << 6)

#define DDRC_ST_DPDN		(1 << 5) /* 0 DDR memory is NOT in deep-power-down state
					    1 DDR memory is in deep-power-down state */
#define DDRC_ST_PDN		(1 << 4) /* 0 DDR memory is NOT in power-down state
					    1 DDR memory is in power-down state */
#define DDRC_ST_AREF		(1 << 3) /* 0 DDR memory is NOT in auto-refresh state
					    1 DDR memory is in auto-refresh state */
#define DDRC_ST_SREF		(1 << 2) /* 0 DDR memory is NOT in self-refresh state
					    1 DDR memory is in self-refresh state */
#define DDRC_ST_CKE1		(1 << 1) /* 0 CKE1 Pin is low
					    1 CKE1 Pin is high */
#define DDRC_ST_CKE0		(1 << 0) /* 0 CKE0 Pin is low
					    1 CKE0 Pin is high */

/* DDRC Configure Register */
#define DDRC_CFG_RDPRI		(1 << 29)
#define DDRC_CFG_ROW1_BIT	27 /* Row Address width. */
#define DDRC_CFG_COL1_BIT	25 /* Row Address width. */
#define DDRC_CFG_BA1		(1 << 24)
#define DDRC_CFG_IMBA		(1 << 23)
#define DDRC_CFG_DQSMD		(1 << 22)
#define DDRC_CFG_BTRUN		(1 << 21)

#define DDRC_CFG_MISPE		(1 << 15)

#define DDRC_CFG_TYPE_BIT	12
#define DDRC_CFG_TYPE_MASK	(0x7 << DDRC_CFG_TYPE_BIT)
#define DDRC_CFG_TYPE_DDR1	(2 << DDRC_CFG_TYPE_BIT)
#define DDRC_CFG_TYPE_MDDR	(3 << DDRC_CFG_TYPE_BIT)
#define DDRC_CFG_TYPE_DDR2	(4 << DDRC_CFG_TYPE_BIT)

#define DDRC_CFG_ROW_BIT	10 /* Row Address width. */
#define DDRC_CFG_ROW_MASK	(0x3 << DDRC_CFG_ROW_BIT)
#define DDRC_CFG_ROW_12		(0 << DDRC_CFG_ROW_BIT) /* 12-bit row address is used */
#define DDRC_CFG_ROW_13		(1 << DDRC_CFG_ROW_BIT) /* 13-bit row address is used */
#define DDRC_CFG_ROW_14		(2 << DDRC_CFG_ROW_BIT) /* 14-bit row address is used */

#define DDRC_CFG_COL_BIT	8 /* Column Address width.
				     Specify the Column address width of external DDR. */
#define DDRC_CFG_COL_MASK	(0x3 << DDRC_CFG_COL_BIT)
#define DDRC_CFG_COL_8		(0 << DDRC_CFG_COL_BIT) /* 8-bit Column address is used */
#define DDRC_CFG_COL_9		(1 << DDRC_CFG_COL_BIT) /* 9-bit Column address is used */
#define DDRC_CFG_COL_10		(2 << DDRC_CFG_COL_BIT) /* 10-bit Column address is used */
#define DDRC_CFG_COL_11		(3 << DDRC_CFG_COL_BIT) /* 11-bit Column address is used */

#define DDRC_CFG_CS1EN		(1 << 7) /* 0 DDR Pin CS1 un-used
					    1 There're DDR memory connected to CS1 */
#define DDRC_CFG_CS0EN		(1 << 6) /* 0 DDR Pin CS0 un-used
					    1 There're DDR memory connected to CS0 */

#define DDRC_CFG_CL_BIT		2 /* CAS Latency */
#define DDRC_CFG_CL_MASK	(0xf << DDRC_CFG_CL_BIT)
#define DDRC_CFG_CL_3		(0x0a << DDRC_CFG_CL_BIT) /* CL = 3 tCK */
#define DDRC_CFG_CL_4		(0x0b << DDRC_CFG_CL_BIT) /* CL = 4 tCK */
#define DDRC_CFG_CL_5		(0x0c << DDRC_CFG_CL_BIT) /* CL = 5 tCK */
#define DDRC_CFG_CL_6		(0x0d << DDRC_CFG_CL_BIT) /* CL = 6 tCK */
#define DDRC_CFG_CL_7           (0x0e << DDRC_CFG_CL_BIT) /* CL = 7 tCK */

#define DDRC_CFG_BA		(1 << 1) /* 0 4 bank device, Pin ba[1:0] valid, ba[2] un-used
					    1 8 bank device, Pin ba[2:0] valid*/
#define DDRC_CFG_DW		(1 << 0) /*0 External memory data width is 16-bit
					   1 External memory data width is 32-bit */

/* DDRC Control Register */
#define DDRC_CTRL_ACTPD		(1 << 15) /* 0 Precharge all banks before entering power-down
					     1 Do not precharge banks before entering power-down */
#define DDRC_CTRL_PDT_BIT	12 /* Power-Down Timer */
#define DDRC_CTRL_PDT_MASK	(0x7 << DDRC_CTRL_PDT_BIT)
#define DDRC_CTRL_PDT_DIS	(0 << DDRC_CTRL_PDT_BIT) /* power-down disabled */
#define DDRC_CTRL_PDT_8		(1 << DDRC_CTRL_PDT_BIT) /* Enter power-down after 8 tCK idle */
#define DDRC_CTRL_PDT_16	(2 << DDRC_CTRL_PDT_BIT) /* Enter power-down after 16 tCK idle */
#define DDRC_CTRL_PDT_32	(3 << DDRC_CTRL_PDT_BIT) /* Enter power-down after 32 tCK idle */
#define DDRC_CTRL_PDT_64	(4 << DDRC_CTRL_PDT_BIT) /* Enter power-down after 64 tCK idle */
#define DDRC_CTRL_PDT_128	(5 << DDRC_CTRL_PDT_BIT) /* Enter power-down after 128 tCK idle */

#define DDRC_CTRL_PRET_BIT	8 /* Precharge Timer */
#define DDRC_CTRL_PRET_MASK	(0x7 << DDRC_CTRL_PRET_BIT) /*  */
  #define DDRC_CTRL_PRET_DIS	(0 << DDRC_CTRL_PRET_BIT) /* PRET function Disabled */
  #define DDRC_CTRL_PRET_8	(1 << DDRC_CTRL_PRET_BIT) /* Precharge active bank after 8 tCK idle */
  #define DDRC_CTRL_PRET_16	(2 << DDRC_CTRL_PRET_BIT) /* Precharge active bank after 16 tCK idle */
  #define DDRC_CTRL_PRET_32	(3 << DDRC_CTRL_PRET_BIT) /* Precharge active bank after 32 tCK idle */
  #define DDRC_CTRL_PRET_64	(4 << DDRC_CTRL_PRET_BIT) /* Precharge active bank after 64 tCK idle */
  #define DDRC_CTRL_PRET_128	(5 << DDRC_CTRL_PRET_BIT) /* Precharge active bank after 128 tCK idle */

#define DDRC_CTRL_SR		(1 << 5) /* 1 Drive external DDR device entering self-refresh mode
					    0 Drive external DDR device exiting self-refresh mode */
#define DDRC_CTRL_UNALIGN	(1 << 4) /* 0 Disable unaligned transfer on AXI BUS
					    1 Enable unaligned transfer on AXI BUS */
#define DDRC_CTRL_ALH		(1 << 3) /* Advanced Latency Hiding:
					    0 Disable ALH
					    1 Enable ALH */
#define DDRC_CTRL_CKE		(1 << 1) /* 0 Not set CKE Pin High
					    1 Set CKE Pin HIGH */
#define DDRC_CTRL_RESET		(1 << 0) /* 0 End resetting ddrc_controller
					    1 Resetting ddrc_controller */

/* DDRC Load-Mode-Register */
#define DDRC_LMR_DDR_ADDR_BIT	16 /* When performing a DDR command, DDRC_ADDR[13:0]
				      corresponding to external DDR address Pin A[13:0] */
#define DDRC_LMR_DDR_ADDR_MASK	(0x3fff << DDRC_LMR_DDR_ADDR_BIT)

#define DDRC_LMR_BA_BIT		8 /* When performing a DDR command, BA[2:0]
				     corresponding to external DDR address Pin BA[2:0]. */
#define DDRC_LMR_BA_MASK	(0x7 << DDRC_LMR_BA_BIT)
/* For DDR2 */
#define DDRC_LMR_BA_MRS		(0 << DDRC_LMR_BA_BIT) /* Mode Register set */
#define DDRC_LMR_BA_EMRS1	(1 << DDRC_LMR_BA_BIT) /* Extended Mode Register1 set */
#define DDRC_LMR_BA_EMRS2	(2 << DDRC_LMR_BA_BIT) /* Extended Mode Register2 set */
#define DDRC_LMR_BA_EMRS3	(3 << DDRC_LMR_BA_BIT) /* Extended Mode Register3 set */
/* For mobile DDR */
#define DDRC_LMR_BA_M_MRS	(0 << DDRC_LMR_BA_BIT) /* Mode Register set */
#define DDRC_LMR_BA_M_EMRS	(2 << DDRC_LMR_BA_BIT) /* Extended Mode Register set */
#define DDRC_LMR_BA_M_SR	(1 << DDRC_LMR_BA_BIT) /* Status Register set */

#define DDRC_LMR_CMD_BIT	4
#define DDRC_LMR_CMD_MASK	(0x3 << DDRC_LMR_CMD_BIT)
#define DDRC_LMR_CMD_PREC	(0 << DDRC_LMR_CMD_BIT)/* Precharge one bank/All banks */
#define DDRC_LMR_CMD_AUREF	(1 << DDRC_LMR_CMD_BIT)/* Auto-Refresh */
#define DDRC_LMR_CMD_LMR	(2 << DDRC_LMR_CMD_BIT)/* Load Mode Register */

#define DDRC_LMR_START		(1 << 0) /* 0 No command is performed
					    1 On the posedge of START, perform a command
					    defined by CMD field */
/* DDRC Mode Register Set */
#define DDR_MRS_PD_BIT		(1 << 10) /* Active power down exit time */
#define DDR_MRS_PD_MASK		(1 << DDR_MRS_PD_BIT)
#define DDR_MRS_PD_FAST_EXIT	(0 << 10)
#define DDR_MRS_PD_SLOW_EXIT	(1 << 10)
#define DDR_MRS_WR_BIT		(1 << 9) /* Write Recovery for autoprecharge */
#define DDR_MRS_WR_MASK		(7 << DDR_MRS_WR_BIT)
#define DDR_MRS_DLL_RST		(1 << 8) /* DLL Reset */
#define DDR_MRS_TM_BIT		7        /* Operating Mode */
#define DDR_MRS_TM_MASK		(1 << DDR_MRS_OM_BIT)
#define DDR_MRS_TM_NORMAL	(0 << DDR_MRS_OM_BIT)
#define DDR_MRS_TM_TEST	(1 << DDR_MRS_OM_BIT)
#define DDR_MRS_CAS_BIT		4        /* CAS Latency */
#define DDR_MRS_CAS_MASK	(7 << DDR_MRS_CAS_BIT)
#define DDR_MRS_BT_BIT		3        /* Burst Type */
#define DDR_MRS_BT_MASK		(1 << DDR_MRS_BT_BIT)
#define DDR_MRS_BT_SEQ	(0 << DDR_MRS_BT_BIT) /* Sequential */
#define DDR_MRS_BT_INT	(1 << DDR_MRS_BT_BIT) /* Interleave */
#define DDR_MRS_BL_BIT		0        /* Burst Length */
#define DDR_MRS_BL_MASK		(7 << DDR_MRS_BL_BIT)
#define DDR_MRS_BL_4		(2 << DDR_MRS_BL_BIT)
#define DDR_MRS_BL_8		(3 << DDR_MRS_BL_BIT)

/* DDRC Extended Mode Register1 Set */
#define DDR_EMRS1_QOFF		(1<<12) /* 0 Output buffer enabled
					   1 Output buffer disabled */
#define DDR_EMRS1_RDQS_EN	(1<<11) /* 0 Disable
					   1 Enable */
#define DDR_EMRS1_DQS_DIS	(1<<10) /* 0 Enable
					   1 Disable */
#define DDR_EMRS1_OCD_BIT	7 /* Additive Latency 0 -> 6 */
#define DDR_EMRS1_OCD_MASK	(0x7 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_OCD_EXIT		(0 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_OCD_D0		(1 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_OCD_D1		(2 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_OCD_ADJ		(4 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_OCD_DFLT		(7 << DDR_EMRS1_OCD_BIT)
#define DDR_EMRS1_AL_BIT	3 /* Additive Latency 0 -> 6 */
#define DDR_EMRS1_AL_MASK	(7 << DDR_EMRS1_AL_BIT)
#define DDR_EMRS1_RTT_BIT	2 /*  */
#define DDR_EMRS1_RTT_MASK	(0x11 << DDR_EMRS1_DIC_BIT) /* Bit 6, Bit 2 */
#define DDR_EMRS1_DIC_BIT	1        /* Output Driver Impedence Control */
#define DDR_EMRS1_DIC_MASK	(1 << DDR_EMRS1_DIC_BIT) /* 100% */
#define DDR_EMRS1_DIC_NORMAL	(0 << DDR_EMRS1_DIC_BIT) /* 60% */
#define DDR_EMRS1_DIC_HALF	(1 << DDR_EMRS1_DIC_BIT)
#define DDR_EMRS1_DLL_BIT	0        /* DLL Enable  */
#define DDR_EMRS1_DLL_MASK	(1 << DDR_EMRS1_DLL_BIT)
#define DDR_EMRS1_DLL_EN	(0 << DDR_EMRS1_DLL_BIT)
#define DDR_EMRS1_DLL_DIS	(1 << DDR_EMRS1_DLL_BIT)

/* Mobile SDRAM Extended Mode Register */
#define DDR_EMRS_DS_BIT		5	/* Driver strength */
#define DDR_EMRS_DS_MASK	(7 << DDR_EMRS_DS_BIT)
#define DDR_EMRS_DS_FULL	(0 << DDR_EMRS_DS_BIT)	/*Full*/
#define DDR_EMRS_DS_HALF	(1 << DDR_EMRS_DS_BIT)	/*1/2 Strength*/
#define DDR_EMRS_DS_QUTR	(2 << DDR_EMRS_DS_BIT)	/*1/4 Strength*/
#define DDR_EMRS_DS_OCTANT	(3 << DDR_EMRS_DS_BIT)	/*1/8 Strength*/
#define DDR_EMRS_DS_QUTR3	(4 << DDR_EMRS_DS_BIT)	/*3/4 Strength*/

#define DDR_EMRS_PRSR_BIT	0	/* Partial Array Self Refresh */
#define DDR_EMRS_PRSR_MASK	(7 << DDR_EMRS_PRSR_BIT)
#define DDR_EMRS_PRSR_ALL	(0 << DDR_EMRS_PRSR_BIT) /*All Banks*/
#define DDR_EMRS_PRSR_HALF_TL	(1 << DDR_EMRS_PRSR_BIT) /*Half of Total Bank*/
#define DDR_EMRS_PRSR_QUTR_TL	(2 << DDR_EMRS_PRSR_BIT) /*Quarter of Total Bank*/
#define DDR_EMRS_PRSR_HALF_B0	(5 << DDR_EMRS_PRSR_BIT) /*Half of Bank0*/
#define DDR_EMRS_PRSR_QUTR_B0	(6 << DDR_EMRS_PRSR_BIT) /*Quarter of Bank0*/

/* DDRC Timing Config Register 1 */
#define DDRC_TIMING1_TRAS_BIT 	28 /* ACTIVE to PRECHARGE command period (2 * tRAS + 1) */
#define DDRC_TIMING1_TRAS_MASK 	(0xf << DDRC_TIMING1_TRAS_BIT)

#define DDRC_TIMING1_TRTP_BIT		24 /* READ to PRECHARGE command period. */
#define DDRC_TIMING1_TRTP_MASK	(0x3 << DDRC_TIMING1_TRTP_BIT)

#define DDRC_TIMING1_TRP_BIT		20 /* PRECHARGE command period. */
#define DDRC_TIMING1_TRP_MASK 	(0x7 << DDRC_TIMING1_TRP_BIT)

#define DDRC_TIMING1_TRCD_BIT		16 /* ACTIVE to READ or WRITE command period. */
#define DDRC_TIMING1_TRCD_MASK	(0x7 << DDRC_TIMING1_TRCD_BIT)

#define DDRC_TIMING1_TRC_BIT 		12 /* ACTIVE to ACTIVE command period. */
#define DDRC_TIMING1_TRC_MASK 	(0xf << DDRC_TIMING1_TRC_BIT)

#define DDRC_TIMING1_TRRD_BIT		8 /* ACTIVE bank A to ACTIVE bank B command period. */
#define DDRC_TIMING1_TRRD_MASK	(0x3 << DDRC_TIMING1_TRRD_BIT)
#define DDRC_TIMING1_TRRD_DISABLE	(0 << DDRC_TIMING1_TRRD_BIT)
#define DDRC_TIMING1_TRRD_2		(1 << DDRC_TIMING1_TRRD_BIT)
#define DDRC_TIMING1_TRRD_3		(2 << DDRC_TIMING1_TRRD_BIT)
#define DDRC_TIMING1_TRRD_4		(3 << DDRC_TIMING1_TRRD_BIT)

#define DDRC_TIMING1_TWR_BIT 		4 /* WRITE Recovery Time defined by register MR of DDR2 memory */
#define DDRC_TIMING1_TWR_MASK		(0x7 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_1		(0 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_2		(1 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_3		(2 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_4		(3 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_5		(4 << DDRC_TIMING1_TWR_BIT)
#define DDRC_TIMING1_TWR_6		(5 << DDRC_TIMING1_TWR_BIT)

#define DDRC_TIMING1_TWTR_BIT		0 /* WRITE to READ command delay. */
#define DDRC_TIMING1_TWTR_MASK	(0x3 << DDRC_TIMING1_TWTR_BIT)
#define DDRC_TIMING1_TWTR_1		(0 << DDRC_TIMING1_TWTR_BIT)
#define DDRC_TIMING1_TWTR_2		(1 << DDRC_TIMING1_TWTR_BIT)
#define DDRC_TIMING1_TWTR_3		(2 << DDRC_TIMING1_TWTR_BIT)
#define DDRC_TIMING1_TWTR_4		(3 << DDRC_TIMING1_TWTR_BIT)

/* DDRC Timing Config Register 2 */
#define DDRC_TIMING2_TRFC_BIT         24 /* AUTO-REFRESH command period. */
#define DDRC_TIMING2_TRFC_MASK        (0xf << DDRC_TIMING2_TRFC_BIT)
#define DDRC_TIMING2_RWCOV_BIT        19 /* Equal to Tsel of MDELAY. */
#define DDRC_TIMING2_RWCOV_MASK       (0x3 << DDRC_TIMING2_RWCOV_BIT)
#define DDRC_TIMING2_TCKE_BIT         16
#define DDRC_TIMING2_TCKE_MASK        (0x7 << DDRC_TIMING2_TCKE_BIT)
#define DDRC_TIMING2_TMINSR_BIT       8  /* Minimum Self-Refresh / Deep-Power-Down time */
#define DDRC_TIMING2_TMINSR_MASK      (0xf << DDRC_TIMING2_TMINSR_BIT)
#define DDRC_TIMING2_TXP_BIT          4  /* EXIT-POWER-DOWN to next valid command period. */
#define DDRC_TIMING2_TXP_MASK         (0x7 << DDRC_TIMING2_TXP_BIT)
#define DDRC_TIMING2_TMRD_BIT         0  /* Load-Mode-Register to next valid command period. */
#define DDRC_TIMING2_TMRD_MASK        (0x3 << DDRC_TIMING2_TMRD_BIT)

/* DDRC  Auto-Refresh Counter */
#define DDRC_REFCNT_CON_BIT           16 /* Constant value used to compare with CNT value. */
#define DDRC_REFCNT_CON_MASK          (0xff << DDRC_REFCNT_CON_BIT)
#define DDRC_REFCNT_CNT_BIT           8  /* 8-bit counter */
#define DDRC_REFCNT_CNT_MASK          (0xff << DDRC_REFCNT_CNT_BIT)
#define DDRC_REFCNT_CLKDIV_BIT        1  /* Clock Divider for auto-refresh counter. */
#define DDRC_REFCNT_CLKDIV_MASK       (0x7 << DDRC_REFCNT_CLKDIV_BIT)
#define DDRC_REFCNT_REF_EN            (1 << 0) /* Enable Refresh Counter */

/* DDRC DQS Delay Control Register */
#define DDRC_DQS_ERROR                (1 << 29) /* ahb_clk Delay Detect ERROR, read-only. */
#define DDRC_DQS_READY                (1 << 28) /* ahb_clk Delay Detect READY, read-only. */
#define DDRC_DQS_SRDET                (1 << 25)
#define DDRC_DQS_DET                  (1 << 24) /* Start delay detecting. */
#define DDRC_DQS_AUTO                 (1 << 23) /* Hardware auto-detect & set delay line */
#define DDRC_DQS_CLKD_BIT             16 /* CLKD is reference value for setting WDQS and RDQS.*/
#define DDRC_DQS_CLKD_MASK            (0x3f << DDRC_DQS_CLKD_BIT)
#define DDRC_DQS_WDQS_BIT             8  /* Set delay element number to write DQS delay-line. */
#define DDRC_DQS_WDQS_MASK            (0x3f << DDRC_DQS_WDQS_BIT)
#define DDRC_DQS_RDQS_BIT             0  /* Set delay element number to read DQS delay-line. */
#define DDRC_DQS_RDQS_MASK            (0x3f << DDRC_DQS_RDQS_BIT)

/* DDRC DQS Delay Adjust Register */
#define DDRC_DQS_ADJDQSCON_BIT        16
#define DDRC_DQS_ADJDQSCON_MASK       (0xffff << DDRC_DQS_ADJDQSCON_BIT)
#define DDRC_DQS_ADJWSIGN             (1 << 13)
#define DDRC_DQS_ADJWDQS_BIT          8 /* The adjust value for WRITE DQS delay */
#define DDRC_DQS_ADJWDQS_MASK         (0x1f << DDRC_DQS_ADJWDQS_BIT)
#define DDRC_DQS_ADJRSIGN             (1 << 5)
#define DDRC_DQS_ADJRDQS_BIT          0 /* The adjust value for READ DQS delay */
#define DDRC_DQS_ADJRDQS_MASK         (0x1f << DDRC_DQS_ADJRDQS_BIT)

/* DDRC Memory Map Config Register */
#define DDRC_MMAP_BASE_BIT            8 /* base address */
#define DDRC_MMAP_BASE_MASK           (0xff << DDRC_MMAP_BASE_BIT)
#define DDRC_MMAP_MASK_BIT            0 /* address mask */
#define DDRC_MMAP_MASK_MASK           (0xff << DDRC_MMAP_MASK_BIT)

#define DDRC_MMAP0_BASE		     (0x20 << DDRC_MMAP_BASE_BIT)
#define DDRC_MMAP1_BASE_64M	(0x24 << DDRC_MMAP_BASE_BIT) /*when bank0 is 128M*/
#define DDRC_MMAP1_BASE_128M	(0x28 << DDRC_MMAP_BASE_BIT) /*when bank0 is 128M*/
#define DDRC_MMAP1_BASE_256M	(0x30 << DDRC_MMAP_BASE_BIT) /*when bank0 is 128M*/

#define DDRC_MMAP_MASK_64_64	(0xfc << DDRC_MMAP_MASK_BIT)  /*mask for two 128M SDRAM*/
#define DDRC_MMAP_MASK_128_128	(0xf8 << DDRC_MMAP_MASK_BIT)  /*mask for two 128M SDRAM*/
#define DDRC_MMAP_MASK_256_256	(0xf0 << DDRC_MMAP_MASK_BIT)  /*mask for two 128M SDRAM*/

/* DDRC Timing Configure Register 1 */
#define DDRC_DDELAYCTRL1_TSEL_BIT			18
#define DDRC_DDELAYCTRL1_TSEL_MASK			(0x3 << DDRC_DDELAYCTRL1_TSEL_BIT)
#define DDRC_DDELAYCTRL1_MSEL_BIT			16
#define DDRC_DDELAYCTRL1_MSEL_MASK			(0x3 << DDRC_DDELAYCTRL1_MSEL_BIT)
#define DDRC_DDELAYCTRL1_HL				(1 << 15)
#define DDRC_DDELAYCTRL1_QUAR				(1 << 14)
#define DDRC_DDELAYCTRL1_MAUTO				(1 << 6)
#define DDRC_DDELAYCTRL1_MSIGN				(1 << 5)
#define DDRC_DDELAYCTRL1_MASK_DELAY_SEL_ADJ_BIT		0
#define DDRC_DDELAYCTRL1_MASK_DELAY_SEL_ADJ_MASK	(0x1f << DDRC_DDELAYCTRL1_MASK_DELAY_SEL_ADJ_BIT)

/* DDRC Timing Configure Register 2 */
#define DDRC_DDELAYCTRL2_MASK_DELAY_SEL_BIT		0
#define DDRC_DDELAYCTRL2_MASK_DELAY_SEL_MASK		(0x3f << DDRC_DDELAYCTRL2_MASK_DELAY_SEL_BIT)

/* DDRC Multi-media stride Register */
#define DDRC_DSTRB_STRB0_BIT		16
#define DDRC_DSTRB_STRB0_MASK		(0x1fff << DDRC_DSTRB_STRB0_BIT)
#define DDRC_DSTRB_STRB1_BIT		0
#define DDRC_DSTRB_STRB1_MASK		(0x1fff << DDRC_DSTRB_STRB1_BIT)
/* DDRC IO pad control Register */
#define DDRC_PMEMBS0_PDDQS3			(1 << 31)
#define DDRC_PMEMBS0_PDDQS2			(1 << 30)
#define DDRC_PMEMBS0_PDDQS1			(1 << 29)
#define DDRC_PMEMBS0_PDDQS0			(1 << 28)
#define DDRC_PMEMBS0_PDDQ3			(1 << 27)
#define DDRC_PMEMBS0_PDDQ2			(1 << 26)
#define DDRC_PMEMBS0_PDDQ1			(1 << 25)
#define DDRC_PMEMBS0_PDDQ0			(1 << 24)
#define DDRC_PMEMBS0_STDQS3			(1 << 23)
#define DDRC_PMEMBS0_STDQS2			(1 << 22)
#define DDRC_PMEMBS0_STDQS1			(1 << 21)
#define DDRC_PMEMBS0_STDQS0			(1 << 20)
#define DDRC_PMEMBS0_STDQ3			(1 << 19)
#define DDRC_PMEMBS0_STDQ2			(1 << 18)
#define DDRC_PMEMBS0_STDQ1			(1 << 17)
#define DDRC_PMEMBS0_STDQ0			(1 << 16)
#define DDRC_PMEMBS0_PEDQS3			(1 << 15)
#define DDRC_PMEMBS0_PEDQS2 			(1 << 14)
#define DDRC_PMEMBS0_PEDQS1 			(1 << 13)
#define DDRC_PMEMBS0_PEDQS0 			(1 << 12)
#define DDRC_PMEMBS0_PEDQ3			(1 << 11)
#define DDRC_PMEMBS0_PEDQ2			(1 << 10)
#define DDRC_PMEMBS0_PEDQ1			(1 << 9)
#define DDRC_PMEMBS0_PEDQ0			(1 << 8)
#define DDRC_PMEMBS0_PSDQS3			(1 << 7)
#define DDRC_PMEMBS0_PSDQS2			(1 << 6)
#define DDRC_PMEMBS0_PSDQS1			(1 << 5)
#define DDRC_PMEMBS0_PSDQS0			(1 << 4)
#define DDRC_PMEMBS0_PSDQ3			(1 << 3)
#define DDRC_PMEMBS0_PSDQ2			(1 << 2)
#define DDRC_PMEMBS0_PSDQ1			(1 << 1)
#define DDRC_PMEMBS0_PSDQ0			(1 << 0)
/* DDRC IO pad control Register */
#define DDRC_PMEMBS1_IENDQS3			(1 << 31)
#define DDRC_PMEMBS1_IENDQS2			(1 << 30)
#define DDRC_PMEMBS1_IENDQS1			(1 << 29)
#define DDRC_PMEMBS1_IENDQS0			(1 << 28)
#define DDRC_PMEMBS1_IENDQ3			(1 << 27)
#define DDRC_PMEMBS1_IENDQ2			(1 << 26)
#define DDRC_PMEMBS1_IENDQ1			(1 << 25)
#define DDRC_PMEMBS1_IENDQ0			(1 << 24)
#define DDRC_PMEMBS1_SSTL			(1 << 16)

#define DDRC_PMEMBS1_SSELDQS3_BIT		14
#define DDRC_PMEMBS1_SSELDQS3_MASK		(0x3 << DDRC_PMEMBS1_SSELDQS3_BIT)

#define DDRC_PMEMBS1_SSELDQS2_BIT		12
#define DDRC_PMEMBS1_SSELDQS2_MASK		(0x3 << DDRC_PMEMBS1_SSELDQS2_BIT)

#define DDRC_PMEMBS1_SSELDQS1_BIT		10
#define DDRC_PMEMBS1_SSELDQS1_MASK		(0x3 << DDRC_PMEMBS1_SSELDQS1_BIT)

#define DDRC_PMEMBS1_SSELDQS0_BIT		8
#define DDRC_PMEMBS1_SSELDQS0_MASK		(0x3 << DDRC_PMEMBS1_SSELDQS0_BIT)

#define DDRC_PMEMBS1_SSELDQ3_BIT		6
#define DDRC_PMEMBS1_SSELDQ3_MASK		(0x3 << DDRC_PMEMBS1_SSELDQ3_BIT)

#define DDRC_PMEMBS1_SSELDQ2_BIT		4
#define DDRC_PMEMBS1_SSELDQ2_MASK		(0x3 << DDRC_PMEMBS1_SSELDQ2_BIT)

#define DDRC_PMEMBS1_SSELDQ1_BIT		2
#define DDRC_PMEMBS1_SSELDQ1_MASK		(0x3 << DDRC_PMEMBS1_SSELDQ1_BIT)

#define DDRC_PMEMBS1_SSELDQ0_BIT		0
#define DDRC_PMEMBS1_SSELDQ0_MASK		(0x3 << DDRC_PMEMBS1_SSELDQ0_BIT)

/* DDRC IO pad control Register */
#define DDRC_PMEMOSEL_CKSSEL_BIT		18
#define DDRC_PMEMOSEL_CKSSEL_MASK		(0x3 << DDRC_PMEMOSEL_CKSSEL_BIT)

#define DDRC_PMEMOSEL_CKESSEL_BIT		16
#define DDRC_PMEMOSEL_CKESSEL_MASK		(0x3 << DDRC_PMEMOSEL_CKESSEL_BIT)

#define DDRC_PMEMOSEL_ADDRSSEL_BIT		14
#define DDRC_PMEMOSEL_ADDRSSEL_MASK		(0x3 << DDRC_PMEMOSEL_ADDRSSEL_BIT)

#define DDRC_PMEMOSEL_DMSSEL3_BIT		12
#define DDRC_PMEMOSEL_DMSSEL3_MASK		(0x3 << DDRC_PMEMOSEL_DMSSEL3_BIT)

#define DDRC_PMEMOSEL_DMSSEL2_BIT		10
#define DDRC_PMEMOSEL_DMSSEL2_MASK		(0x3 << DDRC_PMEMOSEL_DMSSEL2_BIT)

#define DDRC_PMEMOSEL_DMSSEL1_BIT		8
#define DDRC_PMEMOSEL_DMSSEL1_MASK		(0x3 << DDRC_PMEMOSEL_DMSSEL1_BIT)

#define DDRC_PMEMOSEL_DMSSEL0_BIT		6
#define DDRC_PMEMOSEL_DMSSEL0_MASK		(0x3 << DDRC_PMEMOSEL_DMSSEL0_BIT)

#define DDRC_PMEMOSEL_CMDSSEL_BIT		4
#define DDRC_PMEMOSEL_CMDSSEL_MASK		(0x3 << DDRC_PMEMOSEL_CMDSSEL_BIT)

#define DDRC_PMEMOSEL_CSSSEL1_BIT		2
#define DDRC_PMEMOSEL_CSSSEL1_MASK		(0x3 << DDRC_PMEMOSEL_CSSSEL1_BIT)

#define DDRC_PMEMOSEL_CSSSEL0_BIT		0
#define DDRC_PMEMOSEL_CSSSEL0_MASK		(0x3 << DDRC_PMEMOSEL_CSSSEL0_BIT)

/* DDRC IO pad control Register */
#define DDRC_PMEMOEN_CKOEN			(1 << 14)
#define DDRC_PMEMOEN_BAOEN2			(1 << 13)
#define DDRC_PMEMOEN_BAOEN1			(1 << 12)
#define DDRC_PMEMOEN_BAOEN0			(1 << 11)
#define DDRC_PMEMOEN_AOEN13			(1 << 10)
#define DDRC_PMEMOEN_AOEN12			(1 << 9)
#define DDRC_PMEMOEN_AOEN11_0			(1 << 8)
#define DDRC_PMEMOEN_DMOEN3			(1 << 7)
#define DDRC_PMEMOEN_DMOEN2			(1 << 6)
#define DDRC_PMEMOEN_DMOEN1			(1 << 5)
#define DDRC_PMEMOEN_DMOEN0			(1 << 4)
#define DDRC_PMEMOEN_CMDOEN			(1 << 3)
#define DDRC_PMEMOEN_CSOEN1			(1 << 2)
#define DDRC_PMEMOEN_CSOEN0			(1 << 1)
#define DDRC_PMEMOEN_CKEOEN			(1 << 0)

#ifndef __MIPS_ASSEMBLER

#define DDR_GET_VALUE(x, y)                           \
({                                                    \
        unsigned long value, tmp;                     \
        tmp = x * 1000;                               \
        value = (tmp % y == 0) ? (tmp / y) : (tmp / y + 1); \
                value;                                \
})

#endif /* __MIPS_ASSEMBLER */

#define	EMC_BASE	0xB3410000

/*************************************************************************
 * EMC (External Memory Controller)
 *************************************************************************/
#define EMC_BCR    	(EMC_BASE + 0x00)  /* Bus Control Register */
#define EMC_PMEMBS1     (EMC_BASE + 0x6004)
#define EMC_PMEMBS0     (EMC_BASE + 0x6008)
#define EMC_SMCR0	(EMC_BASE + 0x10)  /* Static Memory Control Register 0 ??? */
#define EMC_SMCR1	(EMC_BASE + 0x14)  /* Static Memory Control Register 1 */
#define EMC_SMCR2	(EMC_BASE + 0x18)  /* Static Memory Control Register 2 */
#define EMC_SMCR3	(EMC_BASE + 0x1c)  /* Static Memory Control Register 3 */
#define EMC_SMCR4	(EMC_BASE + 0x20)  /* Static Memory Control Register 4 */
#define EMC_SMCR5	(EMC_BASE + 0x24)  /* Static Memory Control Register 5 */
#define EMC_SMCR6	(EMC_BASE + 0x28)  /* Static Memory Control Register 6 */
#define EMC_SACR0	(EMC_BASE + 0x30)  /* Static Memory Bank 0 Addr Config Reg */
#define EMC_SACR1	(EMC_BASE + 0x34)  /* Static Memory Bank 1 Addr Config Reg */
#define EMC_SACR2	(EMC_BASE + 0x38)  /* Static Memory Bank 2 Addr Config Reg */
#define EMC_SACR3	(EMC_BASE + 0x3c)  /* Static Memory Bank 3 Addr Config Reg */
#define EMC_SACR4	(EMC_BASE + 0x40)  /* Static Memory Bank 4 Addr Config Reg */
#define EMC_SACR5	(EMC_BASE + 0x44)  /* Static Memory Bank 5 Addr Config Reg */
#define EMC_SACR6	(EMC_BASE + 0x48)  /* Static Memory Bank 6 Addr Config Reg */
#define EMC_NFCSR	(EMC_BASE + 0x50)  /* NAND Flash Control/Status Register */
#define EMC_DMCR	(EMC_BASE + 0x80)  /* DRAM Control Register */
#define EMC_RTCSR	(EMC_BASE + 0x84)  /* Refresh Time Control/Status Register */
#define EMC_RTCNT	(EMC_BASE + 0x88)  /* Refresh Timer Counter */
#define EMC_RTCOR	(EMC_BASE + 0x8c)  /* Refresh Time Constant Register */
#define EMC_DMAR0	(EMC_BASE + 0x90)  /* SDRAM Bank 0 Addr Config Register */
#define EMC_DMAR1	(EMC_BASE + 0x94)  /* SDRAM Bank 1 Addr Config Register */
#define EMC_SDMR0	(EMC_BASE + 0xa000) /* Mode Register of SDRAM bank 0 */

#define REG_EMC_BCR 	REG32(EMC_BCR)
#define REG_EMC_PMEMBS1 REG32(EMC_PMEMBS1)
#define REG_EMC_PMEMBS0	REG32(EMC_PMEMBS0)
#define REG_EMC_SMCR0	REG32(EMC_SMCR0) // ???
#define REG_EMC_SMCR1	REG32(EMC_SMCR1)
#define REG_EMC_SMCR2	REG32(EMC_SMCR2)
#define REG_EMC_SMCR3	REG32(EMC_SMCR3)
#define REG_EMC_SMCR4	REG32(EMC_SMCR4)
#define REG_EMC_SMCR5	REG32(EMC_SMCR5)
#define REG_EMC_SMCR6	REG32(EMC_SMCR6)
#define REG_EMC_SACR0	REG32(EMC_SACR0)
#define REG_EMC_SACR1	REG32(EMC_SACR1)
#define REG_EMC_SACR2	REG32(EMC_SACR2)
#define REG_EMC_SACR3	REG32(EMC_SACR3)
#define REG_EMC_SACR4	REG32(EMC_SACR4)

#define REG_EMC_NFCSR	REG32(EMC_NFCSR)

#define REG_EMC_DMCR	REG32(EMC_DMCR)
#define REG_EMC_RTCSR	REG16(EMC_RTCSR)
#define REG_EMC_RTCNT	REG16(EMC_RTCNT)
#define REG_EMC_RTCOR	REG16(EMC_RTCOR)
#define REG_EMC_DMAR0	REG32(EMC_DMAR0)
#define REG_EMC_DMAR1	REG32(EMC_DMAR1)

/* Bus Control Register */
#define EMC_BCR_BT_SEL_BIT      30
#define EMC_BCR_BT_SEL_MASK     (0x3 << EMC_BCR_BT_SEL_BIT)
#define EMC_BCR_PK_SEL          (1 << 24)
#define EMC_BCR_BSR_MASK          (1 << 2)  /* Nand and SDRAM Bus Share Select: 0, share; 1, unshare */
  #define EMC_BCR_BSR_SHARE       (0 << 2)
  #define EMC_BCR_BSR_UNSHARE     (1 << 2)
#define EMC_BCR_BRE             (1 << 1)
#define EMC_BCR_ENDIAN          (1 << 0)

/* Static Memory Control Register */
#define EMC_SMCR_STRV_BIT	24
#define EMC_SMCR_STRV_MASK	(0x1f << EMC_SMCR_STRV_BIT)
#define EMC_SMCR_TAW_BIT	20
#define EMC_SMCR_TAW_MASK	(0x0f << EMC_SMCR_TAW_BIT)
#define EMC_SMCR_TBP_BIT	16
#define EMC_SMCR_TBP_MASK	(0x0f << EMC_SMCR_TBP_BIT)
#define EMC_SMCR_TAH_BIT	12
#define EMC_SMCR_TAH_MASK	(0x07 << EMC_SMCR_TAH_BIT)
#define EMC_SMCR_TAS_BIT	8
#define EMC_SMCR_TAS_MASK	(0x07 << EMC_SMCR_TAS_BIT)
#define EMC_SMCR_BW_BIT		6
#define EMC_SMCR_BW_MASK	(0x03 << EMC_SMCR_BW_BIT)
  #define EMC_SMCR_BW_8BIT	(0 << EMC_SMCR_BW_BIT)
  #define EMC_SMCR_BW_16BIT	(1 << EMC_SMCR_BW_BIT)
  #define EMC_SMCR_BW_32BIT	(2 << EMC_SMCR_BW_BIT)
#define EMC_SMCR_BCM		(1 << 3)
#define EMC_SMCR_BL_BIT		1
#define EMC_SMCR_BL_MASK	(0x03 << EMC_SMCR_BL_BIT)
  #define EMC_SMCR_BL_4		(0 << EMC_SMCR_BL_BIT)
  #define EMC_SMCR_BL_8		(1 << EMC_SMCR_BL_BIT)
  #define EMC_SMCR_BL_16	(2 << EMC_SMCR_BL_BIT)
  #define EMC_SMCR_BL_32	(3 << EMC_SMCR_BL_BIT)
#define EMC_SMCR_SMT		(1 << 0)

/* Static Memory Bank Addr Config Reg */
#define EMC_SACR_BASE_BIT	8
#define EMC_SACR_BASE_MASK	(0xff << EMC_SACR_BASE_BIT)
#define EMC_SACR_MASK_BIT	0
#define EMC_SACR_MASK_MASK	(0xff << EMC_SACR_MASK_BIT)

/* NAND Flash Control/Status Register */
#define EMC_NFCSR_NFCE4		(1 << 7) /* NAND Flash Enable */
#define EMC_NFCSR_NFE4		(1 << 6) /* NAND Flash FCE# Assertion Enable */
#define EMC_NFCSR_NFCE3		(1 << 5)
#define EMC_NFCSR_NFE3		(1 << 4)
#define EMC_NFCSR_NFCE2		(1 << 3)
#define EMC_NFCSR_NFE2		(1 << 2)
#define EMC_NFCSR_NFCE1		(1 << 1)
#define EMC_NFCSR_NFE1		(1 << 0)
#define EMC_NFCSR_NFE(n)	(1 << (((n)-1)*2))
#define EMC_NFCSR_NFCE(n)	(1 << (((n)*2)-1))

/* DRAM Control Register */
#define EMC_DMCR_BW_BIT		31
#define EMC_DMCR_BW		(1 << EMC_DMCR_BW_BIT)
#define EMC_DMCR_CA_BIT		26
#define EMC_DMCR_CA_MASK	(0x07 << EMC_DMCR_CA_BIT)
  #define EMC_DMCR_CA_8		(0 << EMC_DMCR_CA_BIT)
  #define EMC_DMCR_CA_9		(1 << EMC_DMCR_CA_BIT)
  #define EMC_DMCR_CA_10	(2 << EMC_DMCR_CA_BIT)
  #define EMC_DMCR_CA_11	(3 << EMC_DMCR_CA_BIT)
  #define EMC_DMCR_CA_12	(4 << EMC_DMCR_CA_BIT)
#define EMC_DMCR_RMODE		(1 << 25)
#define EMC_DMCR_RFSH		(1 << 24)
#define EMC_DMCR_MRSET		(1 << 23)
#define EMC_DMCR_RA_BIT		20
#define EMC_DMCR_RA_MASK	(0x03 << EMC_DMCR_RA_BIT)
  #define EMC_DMCR_RA_11	(0 << EMC_DMCR_RA_BIT)
  #define EMC_DMCR_RA_12	(1 << EMC_DMCR_RA_BIT)
  #define EMC_DMCR_RA_13	(2 << EMC_DMCR_RA_BIT)
#define EMC_DMCR_BA_BIT		19
#define EMC_DMCR_BA		(1 << EMC_DMCR_BA_BIT)
#define EMC_DMCR_PDM		(1 << 18)
#define EMC_DMCR_EPIN		(1 << 17)
#define EMC_DMCR_MBSEL		(1 << 16)
#define EMC_DMCR_TRAS_BIT	13
#define EMC_DMCR_TRAS_MASK	(0x07 << EMC_DMCR_TRAS_BIT)
#define EMC_DMCR_RCD_BIT	11
#define EMC_DMCR_RCD_MASK	(0x03 << EMC_DMCR_RCD_BIT)
#define EMC_DMCR_TPC_BIT	8
#define EMC_DMCR_TPC_MASK	(0x07 << EMC_DMCR_TPC_BIT)
#define EMC_DMCR_TRWL_BIT	5
#define EMC_DMCR_TRWL_MASK	(0x03 << EMC_DMCR_TRWL_BIT)
#define EMC_DMCR_TRC_BIT	2
#define EMC_DMCR_TRC_MASK	(0x07 << EMC_DMCR_TRC_BIT)
#define EMC_DMCR_TCL_BIT	0
#define EMC_DMCR_TCL_MASK	(0x03 << EMC_DMCR_TCL_BIT)

/* Refresh Time Control/Status Register */
#define EMC_RTCSR_SFR		(1 << 8)    /* self refresh flag */
#define EMC_RTCSR_CMF		(1 << 7)
#define EMC_RTCSR_CKS_BIT	0
#define EMC_RTCSR_CKS_MASK	(0x07 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_DISABLE	(0 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_4	(1 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_16	(2 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_64	(3 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_256	(4 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_1024	(5 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_2048	(6 << EMC_RTCSR_CKS_BIT)
  #define EMC_RTCSR_CKS_4096	(7 << EMC_RTCSR_CKS_BIT)

/* SDRAM Bank Address Configuration Register */
#define EMC_DMAR_BASE_BIT	8
#define EMC_DMAR_BASE_MASK	(0xff << EMC_DMAR_BASE_BIT)
#define EMC_DMAR_MASK_BIT	0
#define EMC_DMAR_MASK_MASK	(0xff << EMC_DMAR_MASK_BIT)

/* Mode Register of SDRAM bank 0 */
#define EMC_SDMR_BM		(1 << 9) /* Write Burst Mode */
#define EMC_SDMR_OM_BIT		7        /* Operating Mode */
#define EMC_SDMR_OM_MASK	(3 << EMC_SDMR_OM_BIT)
  #define EMC_SDMR_OM_NORMAL	(0 << EMC_SDMR_OM_BIT)
#define EMC_SDMR_CAS_BIT	4        /* CAS Latency */
#define EMC_SDMR_CAS_MASK	(7 << EMC_SDMR_CAS_BIT)
  #define EMC_SDMR_CAS_1	(1 << EMC_SDMR_CAS_BIT)
  #define EMC_SDMR_CAS_2	(2 << EMC_SDMR_CAS_BIT)
  #define EMC_SDMR_CAS_3	(3 << EMC_SDMR_CAS_BIT)
#define EMC_SDMR_BT_BIT		3        /* Burst Type */
#define EMC_SDMR_BT_MASK	(1 << EMC_SDMR_BT_BIT)
  #define EMC_SDMR_BT_SEQ	(0 << EMC_SDMR_BT_BIT) /* Sequential */
  #define EMC_SDMR_BT_INT	(1 << EMC_SDMR_BT_BIT) /* Interleave */
#define EMC_SDMR_BL_BIT		0        /* Burst Length */
#define EMC_SDMR_BL_MASK	(7 << EMC_SDMR_BL_BIT)
  #define EMC_SDMR_BL_1		(0 << EMC_SDMR_BL_BIT)
  #define EMC_SDMR_BL_2		(1 << EMC_SDMR_BL_BIT)
  #define EMC_SDMR_BL_4		(2 << EMC_SDMR_BL_BIT)
  #define EMC_SDMR_BL_8		(3 << EMC_SDMR_BL_BIT)

#define EMC_SDMR_CAS2_16BIT \
  (EMC_SDMR_CAS_2 | EMC_SDMR_BT_SEQ | EMC_SDMR_BL_2)
#define EMC_SDMR_CAS2_32BIT \
  (EMC_SDMR_CAS_2 | EMC_SDMR_BT_SEQ | EMC_SDMR_BL_4)
#define EMC_SDMR_CAS3_16BIT \
  (EMC_SDMR_CAS_3 | EMC_SDMR_BT_SEQ | EMC_SDMR_BL_2)
#define EMC_SDMR_CAS3_32BIT \
  (EMC_SDMR_CAS_3 | EMC_SDMR_BT_SEQ | EMC_SDMR_BL_4)

#define	I2C0_BASE	0xB0050000
#define	I2C1_BASE	0xB0051000

/*************************************************************************
 * I2C
 *************************************************************************/
#define	I2C_CTRL(n)		(I2C0_BASE + (n)*0x1000 + 0x00)
#define	I2C_TAR(n)     		(I2C0_BASE + (n)*0x1000 + 0x04)
#define	I2C_SAR(n)     		(I2C0_BASE + (n)*0x1000 + 0x08)
#define	I2C_DC(n)      		(I2C0_BASE + (n)*0x1000 + 0x10)
#define	I2C_SHCNT(n)		(I2C0_BASE + (n)*0x1000 + 0x14)
#define	I2C_SLCNT(n)		(I2C0_BASE + (n)*0x1000 + 0x18)
#define	I2C_FHCNT(n)		(I2C0_BASE + (n)*0x1000 + 0x1C)
#define	I2C_FLCNT(n)		(I2C0_BASE + (n)*0x1000 + 0x20)
#define	I2C_INTST(n)		(I2C0_BASE + (n)*0x1000 + 0x2C)
#define	I2C_INTM(n)		(I2C0_BASE + (n)*0x1000 + 0x30)
#define I2C_RXTL(n)		(I2C0_BASE + (n)*0x1000 + 0x38)
#define I2C_TXTL(n)		(I2C0_BASE + (n)*0x1000 + 0x3c)
#define	I2C_CINTR(n)		(I2C0_BASE + (n)*0x1000 + 0x40)
#define	I2C_CRXUF(n)		(I2C0_BASE + (n)*0x1000 + 0x44)
#define	I2C_CRXOF(n)		(I2C0_BASE + (n)*0x1000 + 0x48)
#define	I2C_CTXOF(n)		(I2C0_BASE + (n)*0x1000 + 0x4C)
#define	I2C_CRXREQ(n)		(I2C0_BASE + (n)*0x1000 + 0x50)
#define	I2C_CTXABRT(n)		(I2C0_BASE + (n)*0x1000 + 0x54)
#define	I2C_CRXDONE(n)		(I2C0_BASE + (n)*0x1000 + 0x58)
#define	I2C_CACT(n)		(I2C0_BASE + (n)*0x1000 + 0x5C)
#define	I2C_CSTP(n)		(I2C0_BASE + (n)*0x1000 + 0x60)
#define	I2C_CSTT(n)		(I2C0_BASE + (n)*0x1000 + 0x64)
#define	I2C_CGC(n)    		(I2C0_BASE + (n)*0x1000 + 0x68)
#define	I2C_ENB(n)     		(I2C0_BASE + (n)*0x1000 + 0x6C)
#define	I2C_STA(n)     		(I2C0_BASE + (n)*0x1000 + 0x70)
#define	I2C_TXABRT(n)		(I2C0_BASE + (n)*0x1000 + 0x80)
#define I2C_DMACR(n)            (I2C0_BASE + (n)*0x1000 + 0x88)
#define I2C_DMATDLR(n)          (I2C0_BASE + (n)*0x1000 + 0x8c)
#define I2C_DMARDLR(n)          (I2C0_BASE + (n)*0x1000 + 0x90)
#define	I2C_SDASU(n)		(I2C0_BASE + (n)*0x1000 + 0x94)
#define	I2C_ACKGC(n)		(I2C0_BASE + (n)*0x1000 + 0x98)
#define	I2C_ENSTA(n)		(I2C0_BASE + (n)*0x1000 + 0x9C)

#define	REG_I2C_CTRL(n)		REG8(I2C_CTRL(n)) /* I2C Control Register (I2C_CTRL) */
#define	REG_I2C_TAR(n)		REG16(I2C_TAR(n)) /* I2C target address (I2C_TAR) */
#define REG_I2C_SAR(n)		REG16(I2C_SAR(n))
#define REG_I2C_DC(n)		REG16(I2C_DC(n))
#define REG_I2C_SHCNT(n)       	REG16(I2C_SHCNT(n))
#define REG_I2C_SLCNT(n)       	REG16(I2C_SLCNT(n))
#define REG_I2C_FHCNT(n)       	REG16(I2C_FHCNT(n))
#define REG_I2C_FLCNT(n)       	REG16(I2C_FLCNT(n))
#define REG_I2C_INTST(n)       	REG16(I2C_INTST(n)) /* i2c interrupt status (I2C_INTST) */
#define REG_I2C_INTM(n)		REG16(I2C_INTM(n)) /* i2c interrupt mask status (I2C_INTM) */
#define REG_I2C_RXTL(n)		REG8(I2C_RXTL(n))
#define REG_I2C_TXTL(n)		REG8(I2C_TXTL(n))
#define REG_I2C_CINTR(n)       	REG8(I2C_CINTR(n))
#define REG_I2C_CRXUF(n)       	REG8(I2C_CRXUF(n))
#define REG_I2C_CRXOF(n)       	REG8(I2C_CRXOF(n))
#define REG_I2C_CTXOF(n)       	REG8(I2C_CTXOF(n))
#define REG_I2C_CRXREQ(n)      	REG8(I2C_CRXREQ(n))
#define REG_I2C_CTXABRT(n)     	REG8(I2C_CTXABRT(n))
#define REG_I2C_CRXDONE(n)     	REG8(I2C_CRXDONE(n))
#define REG_I2C_CACT(n)		REG8(I2C_CACT(n))
#define REG_I2C_CSTP(n)		REG8(I2C_CSTP(n))
#define REG_I2C_CSTT(n)		REG16(I2C_CSTT(n))
#define REG_I2C_CGC(n)		REG8(I2C_CGC(n))
#define REG_I2C_ENB(n)		REG8(I2C_ENB(n))
#define REG_I2C_STA(n)		REG8(I2C_STA(n))
#define REG_I2C_TXABRT(n)      	REG16(I2C_TXABRT(n))
#define REG_I2C_DMACR(n)        REG8(I2C_DMACR(n))
#define REG_I2C_DMATDLR(n)      REG8(I2C_DMATDLR(n))
#define REG_I2C_DMARDLR(n)      REG8(I2C_DMARDLR(n))
#define REG_I2C_SDASU(n)       	REG8(I2C_SDASU(n))
#define REG_I2C_ACKGC(n)       	REG8(I2C_ACKGC(n))
#define REG_I2C_ENSTA(n)       	REG8(I2C_ENSTA(n))

/* I2C Control Register (I2C_CTRL) */

#define I2C_CTRL_STPHLD		(1 << 7) /* Stop Hold Enable bit: when tx fifo empty, 0: send stop 1: never send stop*/
#define I2C_CTRL_SLVDIS		(1 << 6) /* after reset slave is disabled*/
#define I2C_CTRL_REST		(1 << 5)
#define I2C_CTRL_MATP		(1 << 4) /* 1: 10bit address 0: 7bit addressing*/
#define I2C_CTRL_SATP		(1 << 3) /* 1: 10bit address 0: 7bit address*/
#define I2C_CTRL_SPDF		(2 << 1) /* fast mode 400kbps */
#define I2C_CTRL_SPDS		(1 << 1) /* standard mode 100kbps */
#define I2C_CTRL_MD		(1 << 0) /* master enabled*/

/* I2C target address (I2C_TAR) */

#define I2C_TAR_MATP		(1 << 12)
#define I2C_TAR_SPECIAL		(1 << 11)
#define I2C_TAR_GC_OR_START	(1 << 10)
#define I2C_TAR_I2CTAR_BIT	0
#define I2C_TAR_I2CTAR_MASK	(0x3ff << I2C_TAR_I2CTAR_BIT)

/* I2C slave address  */
#define I2C_SAR_I2CSAR_BIT	0
#define I2C_SAR_I2CSAR_MASK	(0x3ff << I2C_SAR_I2CSAR_BIT)

/* I2C data buffer and command (I2C_DC) */

#define I2C_DC_CMD			(1 << 8) /* 1 read 0  write*/
#define I2C_DC_DAT_BIT		0
#define I2C_DC_DAT_MASK		(0xff << I2C_DC_DAT_BIT) /* 1 read 0  write*/

/* i2c interrupt status (I2C_INTST) */

#define I2C_INTST_IGC			(1 << 11) /* */
#define I2C_INTST_ISTT			(1 << 10)
#define I2C_INTST_ISTP			(1 << 9)
#define I2C_INTST_IACT			(1 << 8)
#define I2C_INTST_RXDN			(1 << 7)
#define I2C_INTST_TXABT			(1 << 6)
#define I2C_INTST_RDREQ			(1 << 5)
#define I2C_INTST_TXEMP			(1 << 4)
#define I2C_INTST_TXOF			(1 << 3)
#define I2C_INTST_RXFL			(1 << 2)
#define I2C_INTST_RXOF			(1 << 1)
#define I2C_INTST_RXUF			(1 << 0)

/* i2c interrupt mask status (I2C_INTM) */

#define I2C_INTM_MIGC			(1 << 11) /* */
#define I2C_INTM_MISTT			(1 << 10)
#define I2C_INTM_MISTP			(1 << 9)
#define I2C_INTM_MIACT			(1 << 8)
#define I2C_INTM_MRXDN			(1 << 7)
#define I2C_INTM_MTXABT			(1 << 6)
#define I2C_INTM_MRDREQ			(1 << 5)
#define I2C_INTM_MTXEMP			(1 << 4)
#define I2C_INTM_MTXOF			(1 << 3)
#define I2C_INTM_MRXFL			(1 << 2)
#define I2C_INTM_MRXOF			(1 << 1)
#define I2C_INTM_MRXUF			(1 << 0)

/* I2C Clear Combined and Individual Interrupts (I2C_CINTR) */

#define I2C_CINTR_CINT 			(1 << 0)

/* I2C Clear TX_OVER Interrupt */
/* I2C Clear RDREQ Interrupt */
/* I2C Clear TX_ABRT Interrupt */
/* I2C Clear RX_DONE Interrupt */
/* I2C Clear ACTIVITY Interrupt */
/* I2C Clear STOP Interrupts */
/* I2C Clear START Interrupts */
/* I2C Clear GEN_CALL Interrupts */

/* I2C Enable (I2C_ENB) */

#define I2C_ENB_I2CENB 		(1 << 0) /* Enable the i2c */

/* I2C Status Register (I2C_STA) */

#define I2C_STA_SLVACT		(1 << 6) /* Slave FSM is not in IDLE state */
#define I2C_STA_MSTACT		(1 << 5) /* Master FSM is not in IDLE state */
#define I2C_STA_RFF		(1 << 4) /* RFIFO if full */
#define I2C_STA_RFNE		(1 << 3) /* RFIFO is not empty */
#define I2C_STA_TFE		(1 << 2) /* TFIFO is empty */
#define I2C_STA_TFNF		(1 << 1) /* TFIFO is not full  */
#define I2C_STA_ACT		(1 << 0) /* I2C Activity Status */

/* I2C Transmit Abort Status Register (I2C_TXABRT) */

#define I2C_TXABRT_SLVRD_INTX		(1 << 15)
#define I2C_TXABRT_SLV_ARBLOST		(1 << 14)
#define I2C_TXABRT_SLVFLUSH_TXFIFO	(1 << 13)
#define I2C_TXABRT_ARB_LOST		(1 << 12)
#define I2C_TXABRT_ABRT_MASTER_DIS	(1 << 11)
#define I2C_TXABRT_ABRT_10B_RD_NORSTRT	(1 << 10)
#define I2C_TXABRT_SBYTE_NORSTRT	(1 << 9)
#define I2C_TXABRT_ABRT_HS_NORSTRT	(1 << 8)
#define I2C_TXABRT_SBYTE_ACKDET		(1 << 7)
#define I2C_TXABRT_ABRT_HS_ACKD		(1 << 6)
#define I2C_TXABRT_ABRT_GCALL_READ	(1 << 5)
#define I2C_TXABRT_ABRT_GCALL_NOACK	(1 << 4)
#define I2C_TXABRT_ABRT_XDATA_NOACK	(1 << 3)
#define I2C_TXABRT_ABRT_10ADDR2_NOACK	(1 << 2)
#define I2C_TXABRT_ABRT_10ADDR1_NOACK	(1 << 1)
#define I2C_TXABRT_ABRT_7B_ADDR_NOACK	(1 << 0)

/* */
#define I2C_DMACR_TDEN			(1 << 1)
#define I2C_DMACR_RDEN			(1 << 0)

/* */
#define I2C_DMATDLR_TDLR_BIT		0
#define I2C_DMATDLR_TDLR_MASK		(0x1f << I2C_DMATDLR_TDLR_BIT)

/* */
#define I2C_DMARDLR_RDLR_BIT		0
#define I2C_DMARDLR_RDLR_MASK		(0x1f << I2C_DMARDLR_RDLR_BIT)

/* I2C Enable Status Register (I2C_ENSTA) */

#define I2C_ENSTA_SLVRDLST		(1 << 2)
#define I2C_ENSTA_SLVDISB 		(1 << 1)
#define I2C_ENSTA_I2CEN 		(1 << 0) /* when read as 1, i2c is deemed to be in an enabled state
						    when read as 0, i2c is deemed completely inactive. The cpu can
						 safely read this bit anytime .When this bit is read as 0 ,the cpu can
						 safely read SLVRDLST and SLVDISB */

/* I2C standard mode high count register(I2CSHCNT) */
#define I2CSHCNT_ADJUST(n)	(((n) - 8) < 6 ? 6 : ((n) - 8))

/* I2C standard mode low count register(I2CSLCNT) */
#define I2CSLCNT_ADJUST(n)	(((n) - 1) < 8 ? 8 : ((n) - 1))

/* I2C fast mode high count register(I2CFHCNT) */
#define I2CFHCNT_ADJUST(n)	(((n) - 8) < 6 ? 6 : ((n) - 8))

/* I2C fast mode low count register(I2CFLCNT) */
#define I2CFLCNT_ADJUST(n)	(((n) - 1) < 8 ? 8 : ((n) - 1))

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * I2C
 ***************************************************************************/

#define __i2c_enable(n)		( REG_I2C_ENB(n) = 1 )
#define __i2c_disable(n)       	( REG_I2C_ENB(n) = 0 )

#define __i2c_is_enable(n)       ( REG_I2C_ENSTA(n) & I2C_ENB_I2CENB )
#define __i2c_is_disable(n)      ( !(REG_I2C_ENSTA(n) & I2C_ENB_I2CENB) )

#define __i2c_abrt(n)            ( REG_I2C_TXABRT(n) != 0 )
#define __i2c_abrt_intr(n)       (REG_I2C_INTST(n) & I2C_INTST_TXABT)
#define __i2c_master_active(n)   ( REG_I2C_STA(n) & I2C_STA_MSTACT )
#define __i2c_abrt_7b_addr_nack(n)  ( REG_I2C_TXABRT(n) & I2C_TXABRT_ABRT_7B_ADDR_NOACK )
#define __i2c_txfifo_is_empty(n)     ( REG_I2C_STA(n) & I2C_STA_TFE )
#define __i2c_clear_interrupts(ret,n)    ( ret = REG_I2C_CINTR(n) )

#define __i2c_dma_rd_enable(n)        SETREG8(I2C_DMACR(n),1 << 0)
#define __i2c_dma_rd_disable(n)       CLRREG8(I2C_DMACR(n),1 << 0)
#define __i2c_dma_td_enable(n)        SETREG8(I2C_DMACR(n),1 << 1)
#define __i2c_dma_td_disable(n)       CLRREG8(I2C_DMACR(n),1 << 1)

#define __i2c_send_stop(n)           CLRREG8(I2C_CTRL(n), I2C_CTRL_STPHLD)
#define __i2c_nsend_stop(n)          SETREG8(I2C_CTRL(n), I2C_CTRL_STPHLD)

#define __i2c_set_dma_td_level(n,data) OUTREG8(I2C_DMATDLR(n),data)
#define __i2c_set_dma_rd_level(n,data) OUTREG8(I2C_DMARDLR(n),data)

/*
#define __i2c_set_clk(dev_clk, i2c_clk) \
  ( REG_I2C_GR = (dev_clk) / (16*(i2c_clk)) - 1 )
*/

#define __i2c_read(n)		( REG_I2C_DC(n) & 0xff )
#define __i2c_write(val,n)	( REG_I2C_DC(n) = (val) )

#endif /* __MIPS_ASSEMBLER */

#define	IPU_BASE	0xB3080000

/*************************************************************************
 * IPU (Image Processing Unit)
 *************************************************************************/
#define IPU_V_BASE		0xB3080000
#define IPU_P_BASE		0x13080000

/* Register offset */
#define REG_CTRL		0x0  /* IPU Control Register */
#define REG_STATUS		0x4  /* IPU Status Register */
#define REG_D_FMT		0x8  /* Data Format Register */
#define REG_Y_ADDR		0xc  /* Input Y or YUV422 Packaged Data Address Register */
#define REG_U_ADDR		0x10 /* Input U Data Address Register */
#define REG_V_ADDR		0x14 /* Input V Data Address Register */
#define REG_IN_FM_GS		0x18 /* Input Geometric Size Register */
#define REG_Y_STRIDE		0x1c /* Input Y Data Line Stride Register */
#define REG_UV_STRIDE		0x20 /* Input UV Data Line Stride Register */
#define REG_OUT_ADDR		0x24 /* Output Frame Start Address Register */
#define REG_OUT_GS		0x28 /* Output Geometric Size Register */
#define REG_OUT_STRIDE		0x2c /* Output Data Line Stride Register */
#define REG_RSZ_COEF_INDEX	0x30 /* Resize Coefficients Table Index Register */
#define REG_CSC_CO_COEF		0x34 /* CSC C0 Coefficient Register */
#define REG_CSC_C1_COEF		0x38 /* CSC C1 Coefficient Register */
#define REG_CSC_C2_COEF 	0x3c /* CSC C2 Coefficient Register */
#define REG_CSC_C3_COEF 	0x40 /* CSC C3 Coefficient Register */
#define REG_CSC_C4_COEF 	0x44 /* CSC C4 Coefficient Register */
#define HRSZ_LUT_BASE 		0x48 /* Horizontal Resize Coefficients Look Up Table Register group */
#define VRSZ_LUT_BASE 		0x4c /* Virtical Resize Coefficients Look Up Table Register group */
#define REG_CSC_OFSET_PARA	0x50 /* CSC Offset Parameter Register */
#define REG_Y_PHY_T_ADDR	0x54 /* Input Y Physical Table Address Register */
#define REG_U_PHY_T_ADDR	0x58 /* Input U Physical Table Address Register */
#define REG_V_PHY_T_ADDR	0x5c /* Input V Physical Table Address Register */
#define REG_OUT_PHY_T_ADDR	0x60 /* Output Physical Table Address Register */

/* REG_CTRL: IPU Control Register */
#define IPU_CE_SFT	0x0
#define IPU_CE_MSK	0x1
#define IPU_RUN_SFT	0x1
#define IPU_RUN_MSK	0x1
#define HRSZ_EN_SFT	0x2
#define HRSZ_EN_MSK	0x1
#define VRSZ_EN_SFT	0x3
#define VRSZ_EN_MSK	0x1
#define CSC_EN_SFT	0x4
#define CSC_EN_MSK	0x1
#define FM_IRQ_EN_SFT	0x5
#define FM_IRQ_EN_MSK	0x1
#define IPU_RST_SFT	0x6
#define IPU_RST_MSK	0x1
#define H_SCALE_SFT	0x8
#define H_SCALE_MSK	0x1
#define V_SCALE_SFT	0x9
#define V_SCALE_MSK	0x1
#define PKG_SEL_SFT	0xA
#define PKG_SEL_MSK	0x1
#define LCDC_SEL_SFT	0xB
#define LCDC_SEL_MSK	0x1
#define SPAGE_MAP_SFT	0xC
#define SPAGE_MAP_MSK	0x1
#define DPAGE_SEL_SFT	0xD
#define DPAGE_SEL_MSK	0x1
#define DISP_SEL_SFT	0xE
#define DISP_SEL_MSK	0x1
#define FIELD_CONF_EN_SFT 15
#define FIELD_CONF_EN_MSK 1
#define FIELD_SEL_SFT	16
#define FIELD_SEL_MSK	1
#define DFIX_SEL_SFT	17
#define DFIX_SEL_MSK	1

/* REG_STATUS: IPU Status Register */
#define OUT_END_SFT	0x0
#define OUT_END_MSK	0x1
#define FMT_ERR_SFT	0x1
#define FMT_ERR_MSK	0x1
#define SIZE_ERR_SFT	0x2
#define SIZE_ERR_MSK	0x1

/* D_FMT: Data Format Register */
#define IN_FMT_SFT	0x0
#define IN_FMT_MSK 	0x3
#define IN_OFT_SFT 	0x2
#define IN_OFT_MSK 	0x3
#define YUV_PKG_OUT_SFT	0x10
#define YUV_PKG_OUT_MSK	0x7
#define OUT_FMT_SFT 	0x13
#define OUT_FMT_MSK 	0x3
#define RGB_OUT_OFT_SFT	0x15
#define RGB_OUT_OFT_MSK	0x7
#define RGB888_FMT_SFT	0x18
#define RGB888_FMT_MSK	0x1

/* IN_FM_GS: Input Geometric Size Register */
#define IN_FM_H_SFT	0x0
#define IN_FM_H_MSK	0xFFF
#define IN_FM_W_SFT	0x10
#define IN_FM_W_MSK	0xFFF

/* Y_STRIDE: Input Y Data Line Stride Register */
#define Y_S_SFT		0x0
#define Y_S_MSK		0x3FFF

/* UV_STRIDE: Input UV Data Line Stride Register */
#define V_S_SFT		0x0
#define V_S_MSK		0x1FFF
#define U_S_SFT 	0x10
#define U_S_MSK		0x1FFF

/* OUT_GS: Output Geometric Size Register */
#define OUT_FM_H_SFT	0x0
#define OUT_FM_H_MSK	0x1FFF
#define OUT_FM_W_SFT	0x10
#define OUT_FM_W_MSK	0x7FFF

/* OUT_STRIDE: Output Data Line Stride Register */
#define OUT_S_SFT	0x0
#define OUT_S_MSK	0xFFFF

/* RSZ_COEF_INDEX: Resize Coefficients Table Index Register */
#define VE_IDX_SFT	0x0
#define VE_IDX_MSK	0x1F
#define HE_IDX_SFT	0x10
#define HE_IDX_MSK	0x1F

/* CSC_CX_COEF: CSC CX Coefficient Register */
#define CX_COEF_SFT	0x0
#define CX_COEF_MSK	0xFFF

/* HRSZ_LUT_BASE, VRSZ_LUT_BASE: Resize Coefficients Look Up Table Register group */
#define LUT_LEN		20

#define OUT_N_SFT	0x0
#define OUT_N_MSK	0x1
#define IN_N_SFT	0x1
#define IN_N_MSK	0x1
#define W_COEF_SFT	0x2
#define W_COEF_MSK	0x3FF

/* CSC_OFSET_PARA: CSC Offset Parameter Register */
#define CHROM_OF_SFT	0x10
#define CHROM_OF_MSK	0xFF
#define LUMA_OF_SFT	0x00
#define LUMA_OF_MSK	0xFF

#ifndef __MIPS_ASSEMBLER

#if 0
/*************************************************************************
 * IPU (Image Processing Unit)
 *************************************************************************/
#define u32 volatile unsigned long

#define write_reg(reg, val)	\
do {				\
	*(u32 *)(reg) = (val);	\
} while(0)

#define read_reg(reg, off)	(*(u32 *)((reg)+(off)))

#define set_ipu_fmt(rgb_888_out_fmt, rgb_out_oft, out_fmt, yuv_pkg_out, in_oft, in_fmt ) \
({ write_reg( (IPU_V_BASE + REG_D_FMT), ((in_fmt) & IN_FMT_MSK)<<IN_FMT_SFT \
| ((in_oft) & IN_OFT_MSK)<< IN_OFT_SFT \
| ((out_fmt) & OUT_FMT_MSK)<<OUT_FMT_SFT \
| ((yuv_pkg_out) & YUV_PKG_OUT_MSK ) << YUV_PKG_OUT_SFT \
| ((rgb_888_out_fmt) & RGB888_FMT_MSK ) << RGB888_FMT_SFT \
| ((rgb_out_oft) & RGB_OUT_OFT_MSK ) << RGB_OUT_OFT_SFT); \
})
#define set_y_addr(y_addr) \
({ write_reg( (IPU_V_BASE + REG_Y_ADDR), y_addr); \
})
#define set_u_addr(u_addr) \
({ write_reg( (IPU_V_BASE + REG_U_ADDR), u_addr); \
})

#define set_v_addr(v_addr) \
({ write_reg( (IPU_V_BASE + REG_V_ADDR), v_addr); \
})

#define set_y_phy_t_addr(y_phy_t_addr) \
({ write_reg( (IPU_V_BASE + REG_Y_PHY_T_ADDR), y_phy_t_addr); \
})

#define set_u_phy_t_addr(u_phy_t_addr) \
({ write_reg( (IPU_V_BASE + REG_U_PHY_T_ADDR), u_phy_t_addr); \
})

#define set_v_phy_t_addr(v_phy_t_addr) \
({ write_reg( (IPU_V_BASE + REG_V_PHY_T_ADDR), v_phy_t_addr); \
})

#define set_out_phy_t_addr(out_phy_t_addr) \
({ write_reg( (IPU_V_BASE + REG_OUT_PHY_T_ADDR), out_phy_t_addr); \
})

#define set_inframe_gsize(width, height, y_stride, u_stride, v_stride) \
({ write_reg( (IPU_V_BASE + REG_IN_FM_GS), ((width) & IN_FM_W_MSK)<<IN_FM_W_SFT \
| ((height) & IN_FM_H_MSK)<<IN_FM_H_SFT); \
 write_reg( (IPU_V_BASE + REG_Y_STRIDE), ((y_stride) & Y_S_MSK)<<Y_S_SFT); \
 write_reg( (IPU_V_BASE + REG_UV_STRIDE), ((u_stride) & U_S_MSK)<<U_S_SFT \
| ((v_stride) & V_S_MSK)<<V_S_SFT); \
})
#define set_out_addr(out_addr) \
({ write_reg( (IPU_V_BASE + REG_OUT_ADDR), out_addr); \
})
#define set_outframe_gsize(width, height, o_stride) \
({ write_reg( (IPU_V_BASE + REG_OUT_GS), ((width) & OUT_FM_W_MSK)<<OUT_FM_W_SFT \
| ((height) & OUT_FM_H_MSK)<<OUT_FM_H_SFT); \
 write_reg( (IPU_V_BASE + REG_OUT_STRIDE), ((o_stride) & OUT_S_MSK)<<OUT_S_SFT); \
})
#define set_rsz_lut_end(h_end, v_end) \
({ write_reg( (IPU_V_BASE + REG_RSZ_COEF_INDEX), ((h_end) & HE_IDX_MSK)<<HE_IDX_SFT \
| ((v_end) & VE_IDX_MSK)<<VE_IDX_SFT); \
})
#define set_csc_c0(c0_coeff) \
({ write_reg( (IPU_V_BASE + REG_CSC_CO_COEF), ((c0_coeff) & CX_COEF_MSK)<<CX_COEF_SFT); \
})
#define set_csc_c1(c1_coeff) \
({ write_reg( (IPU_V_BASE + REG_CSC_C1_COEF), ((c1_coeff) & CX_COEF_MSK)<<CX_COEF_SFT); \
})
#define set_csc_c2(c2_coeff) \
({ write_reg( (IPU_V_BASE + REG_CSC_C2_COEF), ((c2_coeff) & CX_COEF_MSK)<<CX_COEF_SFT); \
})
#define set_csc_c3(c3_coeff) \
({ write_reg( (IPU_V_BASE + REG_CSC_C3_COEF), ((c3_coeff) & CX_COEF_MSK)<<CX_COEF_SFT); \
})
#define set_csc_c4(c4_coeff) \
({ write_reg( (IPU_V_BASE + REG_CSC_C4_COEF), ((c4_coeff) & CX_COEF_MSK)<<CX_COEF_SFT); \
})
#define set_hrsz_lut_coef(coef, in_n, out_n) \
({ write_reg( (IPU_V_BASE + HRSZ_LUT_BASE ), ((coef) & W_COEF_MSK)<<W_COEF_SFT \
| ((in_n) & IN_N_MSK)<<IN_N_SFT | ((out_n) & OUT_N_MSK)<<OUT_N_SFT); \
})
#define set_vrsz_lut_coef(coef, in_n, out_n) \
({ write_reg( (IPU_V_BASE + VRSZ_LUT_BASE), ((coef) & W_COEF_MSK)<<W_COEF_SFT \
| ((in_n) & IN_N_MSK)<<IN_N_SFT | ((out_n) & OUT_N_MSK)<<OUT_N_SFT); \
})

#define set_primary_ctrl(vrsz_en, hrsz_en,csc_en, irq_en) \
({ write_reg( (IPU_V_BASE + REG_CTRL), ((irq_en) & FM_IRQ_EN_MSK)<<FM_IRQ_EN_SFT \
| ((vrsz_en) & VRSZ_EN_MSK)<<VRSZ_EN_SFT \
| ((hrsz_en) & HRSZ_EN_MSK)<<HRSZ_EN_SFT \
| ((csc_en) & CSC_EN_MSK)<<CSC_EN_SFT \
| (read_reg(IPU_V_BASE, REG_CTRL)) \
& ~(CSC_EN_MSK<<CSC_EN_SFT | FM_IRQ_EN_MSK<<FM_IRQ_EN_SFT | VRSZ_EN_MSK<<VRSZ_EN_SFT | HRSZ_EN_MSK<<HRSZ_EN_SFT ) ); \
})

#define set_source_ctrl(pkg_sel, spage_sel) \
({ write_reg( (IPU_V_BASE + REG_CTRL), ((pkg_sel) & PKG_SEL_MSK  )<< PKG_SEL_SFT \
| ((spage_sel) & SPAGE_MAP_MSK )<< SPAGE_MAP_SFT \
| (read_reg(IPU_V_BASE, REG_CTRL)) \
& ~(SPAGE_MAP_MSK << SPAGE_MAP_SFT | PKG_SEL_MSK << PKG_SEL_SFT ) ) ; \
})

#define set_out_ctrl(lcdc_sel, dpage_sel, disp_sel) \
({ write_reg( (IPU_V_BASE + REG_CTRL), ((lcdc_sel) & LCDC_SEL_MSK  )<< LCDC_SEL_SFT \
| ((dpage_sel) & DPAGE_SEL_MSK )<< DPAGE_SEL_SFT \
| ((disp_sel) & DISP_SEL_MSK )<< DISP_SEL_SFT \
| (read_reg(IPU_V_BASE, REG_CTRL)) \
& ~(LCDC_SEL_MSK<< LCDC_SEL_SFT | DPAGE_SEL_MSK << DPAGE_SEL_SFT | DISP_SEL_MSK << DISP_SEL_SFT ) ); \
})

#define set_scale_ctrl(v_scal, h_scal) \
({ write_reg( (IPU_V_BASE + REG_CTRL), ((v_scal) & V_SCALE_MSK)<<V_SCALE_SFT \
| ((h_scal) & H_SCALE_MSK)<<H_SCALE_SFT \
| (read_reg(IPU_V_BASE, REG_CTRL)) & ~(V_SCALE_MSK<<V_SCALE_SFT | H_SCALE_MSK<<H_SCALE_SFT ) ); \
})

#define set_csc_ofset_para(chrom_oft, luma_oft) \
({ write_reg( (IPU_V_BASE + REG_CSC_OFSET_PARA ), ((chrom_oft) & CHROM_OF_MSK ) << CHROM_OF_SFT \
| ((luma_oft) & LUMA_OF_MSK ) << LUMA_OF_SFT ) ; \
})

#define sw_reset_ipu() \
({ write_reg( (IPU_V_BASE + REG_CTRL), (read_reg(IPU_V_BASE, REG_CTRL)) \
| IPU_RST_MSK<<IPU_RST_SFT); \
})
#define enable_ipu() \
({ write_reg( (IPU_V_BASE + REG_CTRL), (read_reg(IPU_V_BASE, REG_CTRL)) | 0x1); \
})
#define disable_ipu() \
({ write_reg( (IPU_V_BASE + REG_CTRL), (read_reg(IPU_V_BASE, REG_CTRL)) & ~0x1); \
})
#define run_ipu() \
({ write_reg( (IPU_V_BASE + REG_CTRL), (read_reg(IPU_V_BASE, REG_CTRL)) | 0x2); \
})
#define stop_ipu() \
({ write_reg( (IPU_V_BASE + REG_CTRL), (read_reg(IPU_V_BASE, REG_CTRL)) & ~0x2); \
})

#define polling_end_flag() \
({ (read_reg(IPU_V_BASE, REG_STATUS)) & 0x01; \
})

#define start_vlut_coef_write() \
({ write_reg( (IPU_V_BASE + VRSZ_LUT_BASE), ( 0x1<<12 ) ); \
})

#define start_hlut_coef_write() \
({ write_reg( (IPU_V_BASE + HRSZ_LUT_BASE), ( 0x01<<12 ) ); \
})

#define clear_end_flag() \
({ write_reg( (IPU_V_BASE + REG_STATUS), 0); \
})
#endif /* #if 0 */

#endif /* __MIPS_ASSEMBLER */

#define	LCD_BASE	0xB3050000
#define	SLCD_BASE	0xB3050000

/*************************************************************************
 * SLCD (Smart LCD Controller)
 *************************************************************************/

#define SLCD_CFG	(SLCD_BASE + 0xA0)  /* SLCD Configure Register */
#define SLCD_CTRL	(SLCD_BASE + 0xA4)  /* SLCD Control Register */
#define SLCD_STATE	(SLCD_BASE + 0xA8)  /* SLCD Status Register */
#define SLCD_DATA	(SLCD_BASE + 0xAC)  /* SLCD Data Register */

#define REG_SLCD_CFG	REG32(SLCD_CFG)
#define REG_SLCD_CTRL	REG8(SLCD_CTRL)
#define REG_SLCD_STATE	REG8(SLCD_STATE)
#define REG_SLCD_DATA	REG32(SLCD_DATA)

/* SLCD Configure Register */
#define SLCD_CFG_DWIDTH_BIT	10
#define SLCD_CFG_DWIDTH_MASK	(0x7 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_18BIT	(0 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_16BIT	(1 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_8BIT_x3	(2 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_8BIT_x2	(3 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_8BIT_x1	(4 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_24BIT	(5 << SLCD_CFG_DWIDTH_BIT)
  #define SLCD_CFG_DWIDTH_9BIT_x2	(7 << SLCD_CFG_DWIDTH_BIT)
#define SLCD_CFG_CWIDTH_BIT	(8)
#define SLCD_CFG_CWIDTH_MASK	(0x3 << SLCD_CFG_CWIDTH_BIT)
#define SLCD_CFG_CWIDTH_16BIT	(0 << SLCD_CFG_CWIDTH_BIT)
#define SLCD_CFG_CWIDTH_8BIT	(1 << SLCD_CFG_CWIDTH_BIT)
#define SLCD_CFG_CWIDTH_18BIT	(2 << SLCD_CFG_CWIDTH_BIT)
#define SLCD_CFG_CWIDTH_24BIT	(3 << SLCD_CFG_CWIDTH_BIT)
#define SLCD_CFG_CS_ACTIVE_LOW	(0 << 4)
#define SLCD_CFG_CS_ACTIVE_HIGH	(1 << 4)
#define SLCD_CFG_RS_CMD_LOW	(0 << 3)
#define SLCD_CFG_RS_CMD_HIGH	(1 << 3)
#define SLCD_CFG_CLK_ACTIVE_FALLING	(0 << 1)
#define SLCD_CFG_CLK_ACTIVE_RISING	(1 << 1)
#define SLCD_CFG_TYPE_PARALLEL	(0 << 0)
#define SLCD_CFG_TYPE_SERIAL	(1 << 0)

/* SLCD Control Register */
#define SLCD_CTRL_DMA_MODE	(1 << 2)
#define SLCD_CTRL_DMA_START	(1 << 1)
#define SLCD_CTRL_DMA_EN	(1 << 0)

/* SLCD Status Register */
#define SLCD_STATE_BUSY		(1 << 0)

/* SLCD Data Register */
#define SLCD_DATA_RS_DATA	(0 << 31)
#define SLCD_DATA_RS_COMMAND	(1 << 31)

/*************************************************************************
 * LCD (LCD Controller)
 *************************************************************************/
#define LCD_CFG		(LCD_BASE + 0x00) /* LCD Configure Register */
#define LCD_CTRL	(LCD_BASE + 0x30) /* LCD Control Register */
#define LCD_STATE	(LCD_BASE + 0x34) /* LCD Status Register */

#define LCD_OSDC	(LCD_BASE + 0x100) /* LCD OSD Configure Register */
#define LCD_OSDCTRL	(LCD_BASE + 0x104) /* LCD OSD Control Register */
#define LCD_OSDS	(LCD_BASE + 0x108) /* LCD OSD Status Register */
#define LCD_BGC		(LCD_BASE + 0x10C) /* LCD Background Color Register */
#define LCD_KEY0	(LCD_BASE + 0x110) /* LCD Foreground Color Key Register 0 */
#define LCD_KEY1	(LCD_BASE + 0x114) /* LCD Foreground Color Key Register 1 */
#define LCD_ALPHA	(LCD_BASE + 0x118) /* LCD ALPHA Register */
#define LCD_IPUR	(LCD_BASE + 0x11C) /* LCD IPU Restart Register */
#define LCD_RGBC	(LCD_BASE + 0x90) /* RGB Controll Register */

#define LCD_VAT		(LCD_BASE + 0x0c) /* Virtual Area Setting Register */
#define LCD_DAH		(LCD_BASE + 0x10) /* Display Area Horizontal Start/End Point */
#define LCD_DAV		(LCD_BASE + 0x14) /* Display Area Vertical Start/End Point */

#define LCD_XYP0	(LCD_BASE + 0x120) /* Foreground 0 XY Position Register */
#define LCD_XYP0_PART2	(LCD_BASE + 0x1F0) /* Foreground 0 PART2 XY Position Register */
#define LCD_XYP1	(LCD_BASE + 0x124) /* Foreground 1 XY Position Register */
#define LCD_SIZE0	(LCD_BASE + 0x128) /* Foreground 0 Size Register */
#define LCD_SIZE0_PART2	(LCD_BASE + 0x1F4) /*Foreground 0 PART2 Size Register */
#define LCD_SIZE1	(LCD_BASE + 0x12C) /* Foreground 1 Size Register */

#define LCD_VSYNC	(LCD_BASE + 0x04) /* Vertical Synchronize Register */
#define LCD_HSYNC	(LCD_BASE + 0x08) /* Horizontal Synchronize Register */
#define LCD_PS		(LCD_BASE + 0x18) /* PS Signal Setting */
#define LCD_CLS		(LCD_BASE + 0x1c) /* CLS Signal Setting */
#define LCD_SPL		(LCD_BASE + 0x20) /* SPL Signal Setting */
#define LCD_REV		(LCD_BASE + 0x24) /* REV Signal Setting */
#define LCD_IID		(LCD_BASE + 0x38) /* Interrupt ID Register */
#define LCD_DA0		(LCD_BASE + 0x40) /* Descriptor Address Register 0 */
#define LCD_SA0		(LCD_BASE + 0x44) /* Source Address Register 0 */
#define LCD_FID0	(LCD_BASE + 0x48) /* Frame ID Register 0 */
#define LCD_CMD0	(LCD_BASE + 0x4c) /* DMA Command Register 0 */

#define LCD_OFFS0	(LCD_BASE + 0x60) /* DMA Offsize Register 0 */
#define LCD_PW0		(LCD_BASE + 0x64) /* DMA Page Width Register 0 */
#define LCD_CNUM0	(LCD_BASE + 0x68) /* DMA Command Counter Register 0 */
#define LCD_DESSIZE0	(LCD_BASE + 0x6C) /* Foreground Size in Descriptor 0 Register*/

#define LCD_DA1		(LCD_BASE + 0x50) /* Descriptor Address Register 1 */
#define LCD_SA1		(LCD_BASE + 0x54) /* Source Address Register 1 */
#define LCD_FID1	(LCD_BASE + 0x58) /* Frame ID Register 1 */
#define LCD_CMD1	(LCD_BASE + 0x5c) /* DMA Command Register 1 */
#define LCD_OFFS1	(LCD_BASE + 0x70) /* DMA Offsize Register 1 */
#define LCD_PW1		(LCD_BASE + 0x74) /* DMA Page Width Register 1 */
#define LCD_CNUM1	(LCD_BASE + 0x78) /* DMA Command Counter Register 1 */
#define LCD_DESSIZE1	(LCD_BASE + 0x7C) /* Foreground Size in Descriptor 1 Register*/

#define LCD_DA0_PART2	(LCD_BASE + 0x1C0) /* Descriptor Address Register PART2 */
#define LCD_SA0_PART2	(LCD_BASE + 0x1C4) /* Source Address Register PART2 */
#define LCD_FID0_PART2	(LCD_BASE + 0x1C8) /* Frame ID Register PART2 */
#define LCD_CMD0_PART2	(LCD_BASE + 0x1CC) /* DMA Command Register PART2 */
#define LCD_OFFS0_PART2	(LCD_BASE + 0x1E0) /* DMA Offsize Register PART2 */
#define LCD_PW0_PART2	(LCD_BASE + 0x1E4) /* DMA Command Counter Register PART2 */
#define LCD_CNUM0_PART2	(LCD_BASE + 0x1E8) /* Foreground Size in Descriptor PART2 Register */
#define LCD_DESSIZE0_PART2	(LCD_BASE + 0x1EC) /*  */
#define LCD_PCFG	(LCD_BASE + 0x2C0)

#define REG_LCD_CFG	REG32(LCD_CFG)
#define REG_LCD_CTRL	REG32(LCD_CTRL)
#define REG_LCD_STATE	REG32(LCD_STATE)

#define REG_LCD_OSDC	REG16(LCD_OSDC)
#define REG_LCD_OSDCTRL	REG16(LCD_OSDCTRL)
#define REG_LCD_OSDS	REG16(LCD_OSDS)
#define REG_LCD_BGC	REG32(LCD_BGC)
#define REG_LCD_KEY0	REG32(LCD_KEY0)
#define REG_LCD_KEY1	REG32(LCD_KEY1)
#define REG_LCD_ALPHA	REG8(LCD_ALPHA)
#define REG_LCD_IPUR	REG32(LCD_IPUR)

#define REG_LCD_VAT	REG32(LCD_VAT)
#define REG_LCD_DAH	REG32(LCD_DAH)
#define REG_LCD_DAV	REG32(LCD_DAV)

#define REG_LCD_XYP0		REG32(LCD_XYP0)
#define REG_LCD_XYP0_PART2	REG32(LCD_XYP0_PART2)
#define REG_LCD_XYP1		REG32(LCD_XYP1)
#define REG_LCD_SIZE0		REG32(LCD_SIZE0)
#define REG_LCD_SIZE0_PART2	REG32(LCD_SIZE0_PART2)
#define REG_LCD_SIZE1		REG32(LCD_SIZE1)

#define REG_LCD_RGBC		REG16(LCD_RGBC)

#define REG_LCD_VSYNC		REG32(LCD_VSYNC)
#define REG_LCD_HSYNC		REG32(LCD_HSYNC)
#define REG_LCD_PS		REG32(LCD_PS)
#define REG_LCD_CLS		REG32(LCD_CLS)
#define REG_LCD_SPL		REG32(LCD_SPL)
#define REG_LCD_REV		REG32(LCD_REV)
#define REG_LCD_IID		REG32(LCD_IID)
#define REG_LCD_DA0		REG32(LCD_DA0)
#define REG_LCD_SA0		REG32(LCD_SA0)
#define REG_LCD_FID0		REG32(LCD_FID0)
#define REG_LCD_CMD0		REG32(LCD_CMD0)

#define REG_LCD_OFFS0		REG32(LCD_OFFS0)
#define REG_LCD_PW0		REG32(LCD_PW0)
#define REG_LCD_CNUM0		REG32(LCD_CNUM0)
#define REG_LCD_DESSIZE0	REG32(LCD_DESSIZE0)

#define REG_LCD_DA0_PART2	REG32(LCD_DA0_PART2)
#define REG_LCD_SA0_PART2	REG32(LCD_SA0_PART2)
#define REG_LCD_FID0_PART2	REG32(LCD_FID0_PART2)
#define REG_LCD_CMD0_PART2	REG32(LCD_CMD0_PART2)
#define REG_LCD_OFFS0_PART2	REG32(LCD_OFFS0_PART2)
#define REG_LCD_PW0_PART2	REG32(LCD_PW0_PART2)
#define REG_LCD_CNUM0_PART2	REG32(LCD_CNUM0_PART2)
#define REG_LCD_DESSIZE0_PART2	REG32(LCD_DESSIZE0_PART2)

#define REG_LCD_DA1		REG32(LCD_DA1)
#define REG_LCD_SA1		REG32(LCD_SA1)
#define REG_LCD_FID1		REG32(LCD_FID1)
#define REG_LCD_CMD1		REG32(LCD_CMD1)
#define REG_LCD_OFFS1		REG32(LCD_OFFS1)
#define REG_LCD_PW1		REG32(LCD_PW1)
#define REG_LCD_CNUM1		REG32(LCD_CNUM1)
#define REG_LCD_DESSIZE1	REG32(LCD_DESSIZE1)
#define REG_LCD_PCFG		REG32(LCD_PCFG)

/* LCD Configure Register */
#define LCD_CFG_LCDPIN_BIT	31  /* LCD pins selection */
#define LCD_CFG_LCDPIN_MASK	(0x1 << LCD_CFG_LCDPIN_BIT)
  #define LCD_CFG_LCDPIN_LCD	(0x0 << LCD_CFG_LCDPIN_BIT)
  #define LCD_CFG_LCDPIN_SLCD	(0x1 << LCD_CFG_LCDPIN_BIT)
#define LCD_CFG_TVEPEH		(1 << 30) /* TVE PAL enable extra halfline signal */
//#define LCD_CFG_FUHOLD		(1 << 29) /* hold pixel clock when outFIFO underrun */
#define LCD_CFG_NEWDES		(1 << 28) /* use new descripter. old: 4words, new:8words */
#define LCD_CFG_PALBP		(1 << 27) /* bypass data format and alpha blending */
#define LCD_CFG_TVEN		(1 << 26) /* indicate the terminal is lcd or tv */
#define LCD_CFG_RECOVER		(1 << 25) /* Auto recover when output fifo underrun */
#define LCD_CFG_DITHER		(1 << 24) /* Dither function */
#define LCD_CFG_PSM		(1 << 23) /* PS signal mode */
#define LCD_CFG_CLSM		(1 << 22) /* CLS signal mode */
#define LCD_CFG_SPLM		(1 << 21) /* SPL signal mode */
#define LCD_CFG_REVM		(1 << 20) /* REV signal mode */
#define LCD_CFG_HSYNM		(1 << 19) /* HSYNC signal mode */
#define LCD_CFG_PCLKM		(1 << 18) /* PCLK signal mode */
#define LCD_CFG_INVDAT		(1 << 17) /* Inverse output data */
#define LCD_CFG_SYNDIR_IN	(1 << 16) /* VSYNC&HSYNC direction */
#define LCD_CFG_PSP		(1 << 15) /* PS pin reset state */
#define LCD_CFG_CLSP		(1 << 14) /* CLS pin reset state */
#define LCD_CFG_SPLP		(1 << 13) /* SPL pin reset state */
#define LCD_CFG_REVP		(1 << 12) /* REV pin reset state */
#define LCD_CFG_HSP		(1 << 11) /* HSYNC polarity:0-active high,1-active low */
#define LCD_CFG_PCP		(1 << 10) /* PCLK polarity:0-rising,1-falling */
#define LCD_CFG_DEP		(1 << 9)  /* DE polarity:0-active high,1-active low */
#define LCD_CFG_VSP		(1 << 8)  /* VSYNC polarity:0-rising,1-falling */
#define LCD_CFG_MODE_TFT_18BIT 	(1 << 7)  /* 18bit TFT */
#define LCD_CFG_MODE_TFT_16BIT 	(0 << 7)  /* 16bit TFT */
#define LCD_CFG_MODE_TFT_24BIT 	(1 << 6)  /* 24bit TFT */
#define LCD_CFG_PDW_BIT		4  /* STN pins utilization */
#define LCD_CFG_PDW_MASK	(0x3 << LCD_CFG_PDW_BIT)
#define LCD_CFG_PDW_1		(0 << LCD_CFG_PDW_BIT) /* LCD_D[0] */
  #define LCD_CFG_PDW_2		(1 << LCD_CFG_PDW_BIT) /* LCD_D[0:1] */
  #define LCD_CFG_PDW_4		(2 << LCD_CFG_PDW_BIT) /* LCD_D[0:3]/LCD_D[8:11] */
  #define LCD_CFG_PDW_8		(3 << LCD_CFG_PDW_BIT) /* LCD_D[0:7]/LCD_D[8:15] */
#define LCD_CFG_MODE_BIT	0  /* Display Device Mode Select */
#define LCD_CFG_MODE_MASK	(0x0f << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_GENERIC_TFT	(0 << LCD_CFG_MODE_BIT) /* 16,18 bit TFT */
  #define LCD_CFG_MODE_SPECIAL_TFT_1	(1 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SPECIAL_TFT_2	(2 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SPECIAL_TFT_3	(3 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_NONINTER_CCIR656	(4 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_INTER_CCIR656	(6 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SINGLE_CSTN	(8 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SINGLE_MSTN	(9 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_DUAL_CSTN	(10 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_DUAL_MSTN	(11 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SERIAL_TFT	(12 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_LCM  		(13 << LCD_CFG_MODE_BIT)
  #define LCD_CFG_MODE_SLCD  		LCD_CFG_MODE_LCM

/* LCD Control Register */
#define LCD_CTRL_PINMD		(1 << 31) /* This register set Pin distribution in 16-bit parallel mode
					    0: 16-bit data correspond with LCD_D[15:0]
					    1: 16-bit data correspond with LCD_D[17:10], LCD_D[8:1] */
#define LCD_CTRL_BST_BIT	28  /* Burst Length Selection */
#define LCD_CTRL_BST_MASK	(0x7 << LCD_CTRL_BST_BIT)
  #define LCD_CTRL_BST_4	(0 << LCD_CTRL_BST_BIT) /* 4-word */
  #define LCD_CTRL_BST_8	(1 << LCD_CTRL_BST_BIT) /* 8-word */
  #define LCD_CTRL_BST_16	(2 << LCD_CTRL_BST_BIT) /* 16-word */
  #define LCD_CTRL_BST_32	(3 << LCD_CTRL_BST_BIT) /* 32-word */
  #define LCD_CTRL_BST_C16	(5 << LCD_CTRL_BST_BIT) /* 32-word */
  #define LCD_CTRL_BST_64	(4 << LCD_CTRL_BST_BIT) /* 32-word */
#define LCD_CTRL_RGB565		(0 << 27) /* RGB565 mode(foreground 0 in OSD mode) */
#define LCD_CTRL_RGB555		(1 << 27) /* RGB555 mode(foreground 0 in OSD mode) */
#define LCD_CTRL_OFUP		(1 << 26) /* Output FIFO underrun protection enable */
#define LCD_CTRL_FRC_BIT	24  /* STN FRC Algorithm Selection */
#define LCD_CTRL_FRC_MASK	(0x03 << LCD_CTRL_FRC_BIT)
  #define LCD_CTRL_FRC_16	(0 << LCD_CTRL_FRC_BIT) /* 16 grayscale */
  #define LCD_CTRL_FRC_4	(1 << LCD_CTRL_FRC_BIT) /* 4 grayscale */
  #define LCD_CTRL_FRC_2	(2 << LCD_CTRL_FRC_BIT) /* 2 grayscale */
#define LCD_CTRL_PDD_BIT	16  /* Load Palette Delay Counter */
#define LCD_CTRL_PDD_MASK	(0xff << LCD_CTRL_PDD_BIT)
//#define LCD_CTRL_VGA		(1 << 15) /* VGA interface enable */
#define LCD_CTRL_DACTE		(1 << 14) /* DAC loop back test */
#define LCD_CTRL_EOFM		(1 << 13) /* EOF interrupt mask */
#define LCD_CTRL_SOFM		(1 << 12) /* SOF interrupt mask */
#define LCD_CTRL_OFUM		(1 << 11) /* Output FIFO underrun interrupt mask */
#define LCD_CTRL_IFUM0		(1 << 10) /* Input FIFO 0 underrun interrupt mask */
#define LCD_CTRL_IFUM1		(1 << 9)  /* Input FIFO 1 underrun interrupt mask */
#define LCD_CTRL_LDDM		(1 << 8)  /* LCD disable done interrupt mask */
#define LCD_CTRL_QDM		(1 << 7)  /* LCD quick disable done interrupt mask */
#define LCD_CTRL_BEDN		(1 << 6)  /* Endian selection */
#define LCD_CTRL_PEDN		(1 << 5)  /* Endian in byte:0-msb first, 1-lsb first */
#define LCD_CTRL_DIS		(1 << 4)  /* Disable indicate bit */
#define LCD_CTRL_ENA		(1 << 3)  /* LCD enable bit */
#define LCD_CTRL_BPP_BIT	0  /* Bits Per Pixel */
#define LCD_CTRL_BPP_MASK	(0x07 << LCD_CTRL_BPP_BIT)
  #define LCD_CTRL_BPP_1	(0 << LCD_CTRL_BPP_BIT) /* 1 bpp */
  #define LCD_CTRL_BPP_2	(1 << LCD_CTRL_BPP_BIT) /* 2 bpp */
  #define LCD_CTRL_BPP_4	(2 << LCD_CTRL_BPP_BIT) /* 4 bpp */
  #define LCD_CTRL_BPP_8	(3 << LCD_CTRL_BPP_BIT) /* 8 bpp */
  #define LCD_CTRL_BPP_16	(4 << LCD_CTRL_BPP_BIT) /* 15/16 bpp */
  #define LCD_CTRL_BPP_18_24	(5 << LCD_CTRL_BPP_BIT) /* 18/24/32 bpp */
  #define LCD_CTRL_BPP_CMPS_24	(6 << LCD_CTRL_BPP_BIT) /* 24 compress bpp */
  #define LCD_CTRL_BPP_30	(7 << LCD_CTRL_BPP_BIT) /* 30 bpp */

/* LCD Status Register */
#define LCD_STATE_QD		(1 << 7) /* Quick Disable Done */
#define LCD_STATE_EOF		(1 << 5) /* EOF Flag */
#define LCD_STATE_SOF		(1 << 4) /* SOF Flag */
#define LCD_STATE_OFU		(1 << 3) /* Output FIFO Underrun */
#define LCD_STATE_IFU0		(1 << 2) /* Input FIFO 0 Underrun */
#define LCD_STATE_IFU1		(1 << 1) /* Input FIFO 1 Underrun */
#define LCD_STATE_LDD		(1 << 0) /* LCD Disabled */

/* OSD Configure Register */
#define LCD_OSDC_SOFM1		(1 << 15) /* Start of frame interrupt mask for foreground 1 */
#define LCD_OSDC_EOFM1		(1 << 14) /* End of frame interrupt mask for foreground 1 */
#define LCD_OSDC_OSDIV		(1 << 12)
#define LCD_OSDC_SOFM0		(1 << 11) /* Start of frame interrupt mask for foreground 0 */
#define LCD_OSDC_EOFM0		(1 << 10) /* End of frame interrupt mask for foreground 0 */
#if 0
#define LCD_OSDC_ENDM		(1 << 9) /* End of frame interrupt mask for panel. */
#define LCD_OSDC_F0DIVMD	(1 << 8) /* Divide Foreground 0 into 2 parts.
					  * 0: Foreground 0 only has one part. */
#define LCD_OSDC_F0P1EN		(1 << 7) /* 1: Foreground 0 PART1 is enabled.
					  * 0: Foreground 0 PART1 is disabled. */
#define LCD_OSDC_F0P2MD		(1 << 6) /* 1: PART 1&2 same level and same heighth
					  * 0: PART 1&2 have no same line */
#define LCD_OSDC_F0P2EN		(1 << 5) /* 1: Foreground 0 PART2 is enabled.
					  * 0: Foreground 0 PART2 is disabled.*/
#endif
#define LCD_OSDC_F1EN		(1 << 4) /* enable foreground 1 */
#define LCD_OSDC_F0EN		(1 << 3) /* enable foreground 0 */
#define LCD_OSDC_ALPHAEN		(1 << 2) /* enable alpha blending */
#define LCD_OSDC_ALPHAMD		(1 << 1) /* alpha blending mode */
#define LCD_OSDC_OSDEN		(1 << 0) /* OSD mode enable */

/* OSD Controll Register */
#define LCD_OSDCTRL_IPU		(1 << 15) /* input data from IPU */
#define LCD_OSDCTRL_RGB565	(0 << 4) /* foreground 1, 16bpp, 0-RGB565, 1-RGB555 */
#define LCD_OSDCTRL_RGB555	(1 << 4) /* foreground 1, 16bpp, 0-RGB565, 1-RGB555 */
#define LCD_OSDCTRL_CHANGES	(1 << 3) /* Change size flag */
#define LCD_OSDCTRL_OSDBPP_BIT	0 	 /* Bits Per Pixel of OSD Channel 1 */
#define LCD_OSDCTRL_OSDBPP_MASK	(0x7<<LCD_OSDCTRL_OSDBPP_BIT) 	 /* Bits Per Pixel of OSD Channel 1's MASK */
  #define LCD_OSDCTRL_OSDBPP_16	(4 << LCD_OSDCTRL_OSDBPP_BIT) /* RGB 15,16 bit*/
  #define LCD_OSDCTRL_OSDBPP_15_16	(4 << LCD_OSDCTRL_OSDBPP_BIT) /* RGB 15,16 bit*/
  #define LCD_OSDCTRL_OSDBPP_18_24	(5 << LCD_OSDCTRL_OSDBPP_BIT) /* RGB 18,24 bit*/
  #define LCD_OSDCTRL_OSDBPP_CMPS_24	(6 << LCD_OSDCTRL_OSDBPP_BIT) /* RGB compress 24 bit*/
  #define LCD_OSDCTRL_OSDBPP_30		(7 << LCD_OSDCTRL_OSDBPP_BIT) /* RGB 30 bit*/

/* OSD State Register */
#define LCD_OSDS_SOF1		(1 << 15) /* Start of frame flag for foreground 1 */
#define LCD_OSDS_EOF1		(1 << 14) /* End of frame flag for foreground 1 */
#define LCD_OSDS_SOF0		(1 << 11) /* Start of frame flag for foreground 0 */
#define LCD_OSDS_EOF0		(1 << 10) /* End of frame flag for foreground 0 */
#define LCD_OSDS_READY		(1 << 0)  /* Read for accept the change */

/* Background Color Register */
#define LCD_BGC_RED_OFFSET	(1 << 16)  /* Red color offset */
#define LCD_BGC_RED_MASK	(0xFF<<LCD_BGC_RED_OFFSET)
#define LCD_BGC_GREEN_OFFSET	(1 << 8)   /* Green color offset */
#define LCD_BGC_GREEN_MASK	(0xFF<<LCD_BGC_GREEN_OFFSET)
#define LCD_BGC_BLUE_OFFSET	(1 << 0)   /* Blue color offset */
#define LCD_BGC_BLUE_MASK	(0xFF<<LCD_BGC_BLUE_OFFSET)

/* Foreground Color Key Register 0,1(foreground 0, foreground 1) */
#define LCD_KEY_KEYEN		(1 << 31)   /* enable color key */
#define LCD_KEY_KEYMD		(1 << 30)   /* color key mode */
#define LCD_KEY_RED_OFFSET	16  /* Red color offset */
#define LCD_KEY_RED_MASK	(0xFF<<LCD_KEY_RED_OFFSET)
#define LCD_KEY_GREEN_OFFSET	8   /* Green color offset */
#define LCD_KEY_GREEN_MASK	(0xFF<<LCD_KEY_GREEN_OFFSET)
#define LCD_KEY_BLUE_OFFSET	0   /* Blue color offset */
#define LCD_KEY_BLUE_MASK	(0xFF<<LCD_KEY_BLUE_OFFSET)
#define LCD_KEY_MASK		(LCD_KEY_RED_MASK|LCD_KEY_GREEN_MASK|LCD_KEY_BLUE_MASK)

/* IPU Restart Register */
#define LCD_IPUR_IPUREN		(1 << 31)   /* IPU restart function enable*/
#define LCD_IPUR_IPURMASK	(0xFFFFFF)   /* IPU restart value mask*/

/* RGB Control Register */
#define LCD_RGBC_RGBDM		(1 << 15)   /* enable RGB Dummy data */
#define LCD_RGBC_DMM		(1 << 14)   /* RGB Dummy mode */
#define LCD_RGBC_YCC		(1 << 8)    /* RGB to YCC */
#define LCD_RGBC_ODDRGB_BIT	4	/* odd line serial RGB data arrangement */
#define LCD_RGBC_ODDRGB_MASK	(0x7<<LCD_RGBC_ODDRGB_BIT)
  #define LCD_RGBC_ODD_RGB	0
  #define LCD_RGBC_ODD_RBG	1
  #define LCD_RGBC_ODD_GRB	2
  #define LCD_RGBC_ODD_GBR	3
  #define LCD_RGBC_ODD_BRG	4
  #define LCD_RGBC_ODD_BGR	5
#define LCD_RGBC_EVENRGB_BIT	0	/* even line serial RGB data arrangement */
#define LCD_RGBC_EVENRGB_MASK	(0x7<<LCD_RGBC_EVENRGB_BIT)
  #define LCD_RGBC_EVEN_RGB	0
  #define LCD_RGBC_EVEN_RBG	1
  #define LCD_RGBC_EVEN_GRB	2
  #define LCD_RGBC_EVEN_GBR	3
  #define LCD_RGBC_EVEN_BRG	4
  #define LCD_RGBC_EVEN_BGR	5

/* Vertical Synchronize Register */
#define LCD_VSYNC_VPS_BIT	16  /* VSYNC pulse start in line clock, fixed to 0 */
#define LCD_VSYNC_VPS_MASK	(0xffff << LCD_VSYNC_VPS_BIT)
#define LCD_VSYNC_VPE_BIT	0   /* VSYNC pulse end in line clock */
#define LCD_VSYNC_VPE_MASK	(0xffff << LCD_VSYNC_VPS_BIT)

/* Horizontal Synchronize Register */
#define LCD_HSYNC_HPS_BIT	16  /* HSYNC pulse start position in dot clock */
#define LCD_HSYNC_HPS_MASK	(0xffff << LCD_HSYNC_HPS_BIT)
#define LCD_HSYNC_HPE_BIT	0   /* HSYNC pulse end position in dot clock */
#define LCD_HSYNC_HPE_MASK	(0xffff << LCD_HSYNC_HPE_BIT)

/* Virtual Area Setting Register */
#define LCD_VAT_HT_BIT		16  /* Horizontal Total size in dot clock */
#define LCD_VAT_HT_MASK		(0xffff << LCD_VAT_HT_BIT)
#define LCD_VAT_VT_BIT		0   /* Vertical Total size in dot clock */
#define LCD_VAT_VT_MASK		(0xffff << LCD_VAT_VT_BIT)

/* Display Area Horizontal Start/End Point Register */
#define LCD_DAH_HDS_BIT		16  /* Horizontal display area start in dot clock */
#define LCD_DAH_HDS_MASK	(0xffff << LCD_DAH_HDS_BIT)
#define LCD_DAH_HDE_BIT		0   /* Horizontal display area end in dot clock */
#define LCD_DAH_HDE_MASK	(0xffff << LCD_DAH_HDE_BIT)

/* Display Area Vertical Start/End Point Register */
#define LCD_DAV_VDS_BIT		16  /* Vertical display area start in line clock */
#define LCD_DAV_VDS_MASK	(0xffff << LCD_DAV_VDS_BIT)
#define LCD_DAV_VDE_BIT		0   /* Vertical display area end in line clock */
#define LCD_DAV_VDE_MASK	(0xffff << LCD_DAV_VDE_BIT)

/* Foreground XY Position Register */
#define LCD_XYP_YPOS_BIT	16  /* Y position bit of foreground 0 or 1 */
#define LCD_XYP_YPOS_MASK	(0xffff << LCD_XYP_YPOS_BIT)
#define LCD_XYP_XPOS_BIT	0   /* X position bit of foreground 0 or 1 */
#define LCD_XYP_XPOS_MASK	(0xffff << LCD_XYP_XPOS_BIT)

/* PS Signal Setting */
#define LCD_PS_PSS_BIT		16  /* PS signal start position in dot clock */
#define LCD_PS_PSS_MASK		(0xffff << LCD_PS_PSS_BIT)
#define LCD_PS_PSE_BIT		0   /* PS signal end position in dot clock */
#define LCD_PS_PSE_MASK		(0xffff << LCD_PS_PSE_BIT)

/* CLS Signal Setting */
#define LCD_CLS_CLSS_BIT	16  /* CLS signal start position in dot clock */
#define LCD_CLS_CLSS_MASK	(0xffff << LCD_CLS_CLSS_BIT)
#define LCD_CLS_CLSE_BIT	0   /* CLS signal end position in dot clock */
#define LCD_CLS_CLSE_MASK	(0xffff << LCD_CLS_CLSE_BIT)

/* SPL Signal Setting */
#define LCD_SPL_SPLS_BIT	16  /* SPL signal start position in dot clock */
#define LCD_SPL_SPLS_MASK	(0xffff << LCD_SPL_SPLS_BIT)
#define LCD_SPL_SPLE_BIT	0   /* SPL signal end position in dot clock */
#define LCD_SPL_SPLE_MASK	(0xffff << LCD_SPL_SPLE_BIT)

/* REV Signal Setting */
#define LCD_REV_REVS_BIT	16  /* REV signal start position in dot clock */
#define LCD_REV_REVS_MASK	(0xffff << LCD_REV_REVS_BIT)

/* DMA Command Register */
#define LCD_CMD_SOFINT		(1 << 31)
#define LCD_CMD_EOFINT		(1 << 30)
#define LCD_CMD_CMD		(1 << 29) /* indicate command in slcd mode */
#define LCD_CMD_PAL		(1 << 28)
#define LCD_CMD_UNCOMP_EN	(1 << 27)
#define LCD_CMD_UNCOMPRESS_WITHOUT_ALPHA	(1 << 26)
#define LCD_CMD_LEN_BIT		0
#define LCD_CMD_LEN_MASK	(0xffffff << LCD_CMD_LEN_BIT)

/* DMA Offsize Register 0,1 */

/* DMA Page Width Register 0,1 */

/* DMA Command Counter Register 0,1 */

/* Foreground 0,1 Size Register */
#define LCD_DESSIZE_HEIGHT_BIT	16  /* height of foreground 1 */
#define LCD_DESSIZE_HEIGHT_MASK	(0xffff << LCD_DESSIZE_HEIGHT_BIT)
#define LCD_DESSIZE_WIDTH_BIT	0  /* width of foreground 1 */
#define LCD_DESSIZE_WIDTH_MASK	(0xffff << LCD_DESSIZE_WIDTH_BIT)

/* Priority level threshold configure Register */
#define LCD_PCFG_LCD_PRI_MD	(1 << 31)
#define LCD_PCFG_HP_BST_BIT	28
#define LCD_PCFG_HP_BST_MASK	(0x7 << LCD_PCFG_HP_BST_BIT)
#define LCD_PCFG_PCFG2_BIT	8
#define LCD_PCFG_PCFG2_MASK	(0xf << LCD_PCFG_PCFG2_BIT)
#define LCD_PCFG_PCFG1_BIT	4
#define LCD_PCFG_PCFG1_MASK	(0xf << LCD_PCFG_PCFG1_BIT)
#define LCD_PCFG_PCFG0_BIT	0
#define LCD_PCFG_PCFG0_MASK	(0xf << LCD_PCFG_PCFG0_BIT)

#ifndef __MIPS_ASSEMBLER

/*************************************************************************
 * SLCD (Smart LCD Controller)
 *************************************************************************/
#define __slcd_set_data_18bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_18BIT )
#define __slcd_set_data_16bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_16BIT )
#define __slcd_set_data_8bit_x3() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_8BIT_x3 )
#define __slcd_set_data_8bit_x2() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_8BIT_x2 )
#define __slcd_set_data_8bit_x1() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_8BIT_x1 )
#define __slcd_set_data_24bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_24BIT )
#define __slcd_set_data_9bit_x2() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_DWIDTH_MASK) | SLCD_CFG_DWIDTH_9BIT_x2 )

#define __slcd_set_cmd_16bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_CWIDTH_MASK) | SLCD_CFG_CWIDTH_16BIT )
#define __slcd_set_cmd_8bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_CWIDTH_MASK) | SLCD_CFG_CWIDTH_8BIT )
#define __slcd_set_cmd_18bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_CWIDTH_MASK) | SLCD_CFG_CWIDTH_18BIT )
#define __slcd_set_cmd_24bit() \
  ( REG_SLCD_CFG = (REG_SLCD_CFG & ~SLCD_CFG_CWIDTH_MASK) | SLCD_CFG_CWIDTH_24BIT )

#define __slcd_set_cs_high()        ( REG_SLCD_CFG |= SLCD_CFG_CS_ACTIVE_HIGH )
#define __slcd_set_cs_low()         ( REG_SLCD_CFG &= ~SLCD_CFG_CS_ACTIVE_HIGH )

#define __slcd_set_rs_high()        ( REG_SLCD_CFG |= SLCD_CFG_RS_CMD_HIGH )
#define __slcd_set_rs_low()         ( REG_SLCD_CFG &= ~SLCD_CFG_RS_CMD_HIGH )

#define __slcd_set_clk_falling()    ( REG_SLCD_CFG &= ~SLCD_CFG_CLK_ACTIVE_RISING )
#define __slcd_set_clk_rising()     ( REG_SLCD_CFG |= SLCD_CFG_CLK_ACTIVE_RISING )

#define __slcd_set_parallel_type()  ( REG_SLCD_CFG &= ~SLCD_CFG_TYPE_SERIAL )
#define __slcd_set_serial_type()    ( REG_SLCD_CFG |= SLCD_CFG_TYPE_SERIAL )

/* SLCD Control Register */
#define __slcd_enable_dma()         ( REG_SLCD_CTRL |= SLCD_CTRL_DMA_EN )
#define __slcd_disable_dma()        ( REG_SLCD_CTRL &= ~SLCD_CTRL_DMA_EN )

/* SLCD Status Register */
#define __slcd_is_busy()            ( REG_SLCD_STATE & SLCD_STATE_BUSY )

/* SLCD Data Register */
#define __slcd_set_cmd_rs()         ( REG_SLCD_DATA |= SLCD_DATA_RS_COMMAND)
#define __slcd_set_data_rs()        ( REG_SLCD_DATA &= ~SLCD_DATA_RS_COMMAND)

/***************************************************************************
 * LCD
 ***************************************************************************/
#define __lcd_as_smart_lcd() 		( REG_LCD_CFG |= ( LCD_CFG_LCDPIN_SLCD | LCD_CFG_MODE_SLCD))
#define __lcd_as_general_lcd() 		( REG_LCD_CFG &= ~( LCD_CFG_LCDPIN_SLCD | LCD_CFG_MODE_SLCD))

#define __lcd_enable_tvepeh() 		( REG_LCD_CFG |= LCD_CFG_TVEPEH )
#define __lcd_disable_tvepeh() 		( REG_LCD_CFG &= ~LCD_CFG_TVEPEH )

#define __lcd_enable_fuhold() 		( REG_LCD_CFG |= LCD_CFG_FUHOLD )
#define __lcd_disable_fuhold() 		( REG_LCD_CFG &= ~LCD_CFG_FUHOLD )

#define __lcd_des_8word() 		( REG_LCD_CFG |= LCD_CFG_NEWDES )
#define __lcd_des_4word() 		( REG_LCD_CFG &= ~LCD_CFG_NEWDES )

#define __lcd_enable_bypass_pal() 	( REG_LCD_CFG |= LCD_CFG_PALBP )
#define __lcd_disable_bypass_pal() 	( REG_LCD_CFG &= ~LCD_CFG_PALBP )

#define __lcd_set_lcdpnl_term()		( REG_LCD_CFG |= LCD_CFG_TVEN )
#define __lcd_set_tv_term()		( REG_LCD_CFG &= ~LCD_CFG_TVEN )

#define __lcd_enable_auto_recover() 	( REG_LCD_CFG |= LCD_CFG_RECOVER )
#define __lcd_disable_auto_recover() 	( REG_LCD_CFG &= ~LCD_CFG_RECOVER )

#define __lcd_enable_dither() 	        ( REG_LCD_CFG |= LCD_CFG_DITHER )
#define __lcd_disable_dither() 	        ( REG_LCD_CFG &= ~LCD_CFG_DITHER )

#define __lcd_disable_ps_mode()	        ( REG_LCD_CFG |= LCD_CFG_PSM )
#define __lcd_enable_ps_mode()	        ( REG_LCD_CFG &= ~LCD_CFG_PSM )

#define __lcd_disable_cls_mode() 	( REG_LCD_CFG |= LCD_CFG_CLSM )
#define __lcd_enable_cls_mode()	        ( REG_LCD_CFG &= ~LCD_CFG_CLSM )

#define __lcd_disable_spl_mode() 	( REG_LCD_CFG |= LCD_CFG_SPLM )
#define __lcd_enable_spl_mode()	        ( REG_LCD_CFG &= ~LCD_CFG_SPLM )

#define __lcd_disable_rev_mode() 	( REG_LCD_CFG |= LCD_CFG_REVM )
#define __lcd_enable_rev_mode()	        ( REG_LCD_CFG &= ~LCD_CFG_REVM )

#define __lcd_disable_hsync_mode() 	( REG_LCD_CFG |= LCD_CFG_HSYNM )
#define __lcd_enable_hsync_mode()	( REG_LCD_CFG &= ~LCD_CFG_HSYNM )

#define __lcd_disable_pclk_mode() 	( REG_LCD_CFG |= LCD_CFG_PCLKM )
#define __lcd_enable_pclk_mode()	( REG_LCD_CFG &= ~LCD_CFG_PCLKM )

#define __lcd_normal_outdata()          ( REG_LCD_CFG &= ~LCD_CFG_INVDAT )
#define __lcd_inverse_outdata()         ( REG_LCD_CFG |= LCD_CFG_INVDAT )

#define __lcd_sync_input()              ( REG_LCD_CFG |= LCD_CFG_SYNDIR_IN )
#define __lcd_sync_output()             ( REG_LCD_CFG &= ~LCD_CFG_SYNDIR_IN )

#define __lcd_hsync_active_high()       ( REG_LCD_CFG &= ~LCD_CFG_HSP )
#define __lcd_hsync_active_low()        ( REG_LCD_CFG |= LCD_CFG_HSP )

#define __lcd_pclk_rising()             ( REG_LCD_CFG &= ~LCD_CFG_PCP )
#define __lcd_pclk_falling()            ( REG_LCD_CFG |= LCD_CFG_PCP )

#define __lcd_de_active_high()          ( REG_LCD_CFG &= ~LCD_CFG_DEP )
#define __lcd_de_active_low()           ( REG_LCD_CFG |= LCD_CFG_DEP )

#define __lcd_vsync_rising()            ( REG_LCD_CFG &= ~LCD_CFG_VSP )
#define __lcd_vsync_falling()           ( REG_LCD_CFG |= LCD_CFG_VSP )

#define __lcd_set_16_tftpnl() \
  ( REG_LCD_CFG = (REG_LCD_CFG & ~LCD_CFG_MODE_TFT_MASK) | LCD_CFG_MODE_TFT_16BIT )

#define __lcd_set_18_tftpnl() \
  ( REG_LCD_CFG = (REG_LCD_CFG & ~LCD_CFG_MODE_TFT_MASK) | LCD_CFG_MODE_TFT_18BIT )

#define __lcd_set_24_tftpnl()		( REG_LCD_CFG |= LCD_CFG_MODE_TFT_24BIT )

/*
 * n=1,2,4,8 for single mono-STN
 * n=4,8 for dual mono-STN
 */
#define __lcd_set_panel_datawidth(n) 		\
do { 						\
	REG_LCD_CFG &= ~LCD_CFG_PDW_MASK; 	\
	REG_LCD_CFG |= LCD_CFG_PDW_n##;		\
} while (0)

/* m = LCD_CFG_MODE_GENERUIC_TFT_xxx */
#define __lcd_set_panel_mode(m) 		\
do {						\
	REG_LCD_CFG &= ~LCD_CFG_MODE_MASK;	\
	REG_LCD_CFG |= (m);			\
} while(0)

/* n=4,8,16 */
#define __lcd_set_burst_length(n) 		\
do {						\
	REG_LCD_CTRL &= ~LCD_CTRL_BST_MASK;	\
	REG_LCD_CTRL |= LCD_CTRL_BST_n##;	\
} while (0)

#define __lcd_select_rgb565()		( REG_LCD_CTRL &= ~LCD_CTRL_RGB555 )
#define __lcd_select_rgb555()		( REG_LCD_CTRL |= LCD_CTRL_RGB555 )

#define __lcd_set_ofup()		( REG_LCD_CTRL |= LCD_CTRL_OFUP )
#define __lcd_clr_ofup()		( REG_LCD_CTRL &= ~LCD_CTRL_OFUP )

/* n=2,4,16 */
#define __lcd_set_stn_frc(n) 			\
do {						\
	REG_LCD_CTRL &= ~LCD_CTRL_FRC_MASK;	\
	REG_LCD_CTRL |= LCD_CTRL_FRC_n##;	\
} while (0)

#define __lcd_enable_eof_intr()		( REG_LCD_CTRL |= LCD_CTRL_EOFM )
#define __lcd_disable_eof_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_EOFM )

#define __lcd_enable_sof_intr()		( REG_LCD_CTRL |= LCD_CTRL_SOFM )
#define __lcd_disable_sof_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_SOFM )

#define __lcd_enable_ofu_intr()		( REG_LCD_CTRL |= LCD_CTRL_OFUM )
#define __lcd_disable_ofu_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_OFUM )

#define __lcd_enable_ifu0_intr()	( REG_LCD_CTRL |= LCD_CTRL_IFUM0 )
#define __lcd_disable_ifu0_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_IFUM0 )

#define __lcd_enable_ifu1_intr()	( REG_LCD_CTRL |= LCD_CTRL_IFUM1 )
#define __lcd_disable_ifu1_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_IFUM1 )

#define __lcd_enable_ldd_intr()		( REG_LCD_CTRL |= LCD_CTRL_LDDM )
#define __lcd_disable_ldd_intr()	( REG_LCD_CTRL &= ~LCD_CTRL_LDDM )

#define __lcd_enable_qd_intr()		( REG_LCD_CTRL |= LCD_CTRL_QDM )
#define __lcd_disable_qd_intr()		( REG_LCD_CTRL &= ~LCD_CTRL_QDM )

#define __lcd_reverse_byte_endian()	( REG_LCD_CTRL |= LCD_CTRL_BEDN )
#define __lcd_normal_byte_endian()	( REG_LCD_CTRL &= ~LCD_CTRL_BEDN )

#define __lcd_pixel_endian_little()	( REG_LCD_CTRL |= LCD_CTRL_PEDN )
#define __lcd_pixel_endian_big()	( REG_LCD_CTRL &= ~LCD_CTRL_PEDN )

#define __lcd_set_dis()			( REG_LCD_CTRL |= LCD_CTRL_DIS )
#define __lcd_clr_dis()			( REG_LCD_CTRL &= ~LCD_CTRL_DIS )

#define __lcd_set_ena()			( REG_LCD_CTRL |= LCD_CTRL_ENA )
#define __lcd_clr_ena()			( REG_LCD_CTRL &= ~LCD_CTRL_ENA )

/* n=1,2,4,8,16 */
#define __lcd_set_bpp(n) \
  ( REG_LCD_CTRL = (REG_LCD_CTRL & ~LCD_CTRL_BPP_MASK) | LCD_CTRL_BPP_##n )

/* LCD status register indication */

#define __lcd_quick_disable_done()	( REG_LCD_STATE & LCD_STATE_QD )
#define __lcd_disable_done()		( REG_LCD_STATE & LCD_STATE_LDD )
#define __lcd_infifo0_underrun()	( REG_LCD_STATE & LCD_STATE_IFU0 )
#define __lcd_infifo1_underrun()	( REG_LCD_STATE & LCD_STATE_IFU1 )
#define __lcd_outfifo_underrun()	( REG_LCD_STATE & LCD_STATE_OFU )
#define __lcd_start_of_frame()		( REG_LCD_STATE & LCD_STATE_SOF )
#define __lcd_end_of_frame()		( REG_LCD_STATE & LCD_STATE_EOF )

#define __lcd_clr_outfifounderrun()	( REG_LCD_STATE &= ~LCD_STATE_OFU )
#define __lcd_clr_sof()			( REG_LCD_STATE &= ~LCD_STATE_SOF )
#define __lcd_clr_eof()			( REG_LCD_STATE &= ~LCD_STATE_EOF )

/* OSD functions */
#define __lcd_enable_osd() 	(REG_LCD_OSDC |= LCD_OSDC_OSDEN)
#define __lcd_enable_f0() 	(REG_LCD_OSDC |= LCD_OSDC_F0EN)
#define __lcd_enable_f1()	(REG_LCD_OSDC |= LCD_OSDC_F1EN)
#define __lcd_enable_alpha() 	(REG_LCD_OSDC |= LCD_OSDC_ALPHAEN)
#define __lcd_enable_alphamd()	(REG_LCD_OSDC |= LCD_OSDC_ALPHAMD)

#define __lcd_disable_osd()	(REG_LCD_OSDC &= ~LCD_OSDC_OSDEN)
#define __lcd_disable_f0() 	(REG_LCD_OSDC &= ~LCD_OSDC_F0EN)
#define __lcd_disable_f1() 	(REG_LCD_OSDC &= ~LCD_OSDC_F1EN)
#define __lcd_disable_alpha()	(REG_LCD_OSDC &= ~LCD_OSDC_ALPHAEN)
#define __lcd_disable_alphamd()	(REG_LCD_OSDC &= ~LCD_OSDC_ALPHAMD)

/* OSD Controll Register */
#define __lcd_fg1_use_ipu() 		(REG_LCD_OSDCTRL |= LCD_OSDCTRL_IPU)
#define __lcd_fg1_use_dma_chan1() 	(REG_LCD_OSDCTRL &= ~LCD_OSDCTRL_IPU)
#define __lcd_fg1_unuse_ipu() 		__lcd_fg1_use_dma_chan1()
#define __lcd_osd_rgb555_mode()         ( REG_LCD_OSDCTRL |= LCD_OSDCTRL_RGB555 )
#define __lcd_osd_rgb565_mode()         ( REG_LCD_OSDCTRL &= ~LCD_OSDCTRL_RGB555 )
#define __lcd_osd_change_size()         ( REG_LCD_OSDCTRL |= LCD_OSDCTRL_CHANGES )
#define __lcd_osd_bpp_15_16() \
  ( REG_LCD_OSDCTRL = (REG_LCD_OSDCTRL & ~LCD_OSDCTRL_OSDBPP_MASK) | LCD_OSDCTRL_OSDBPP_15_16 )
#define __lcd_osd_bpp_18_24() \
  ( REG_LCD_OSDCTRL = (REG_LCD_OSDCTRL & ~LCD_OSDCTRL_OSDBPP_MASK) | LCD_OSDCTRL_OSDBPP_18_24 )

/* OSD State Register */
#define __lcd_start_of_fg1()		( REG_LCD_STATE & LCD_OSDS_SOF1 )
#define __lcd_end_of_fg1()		( REG_LCD_STATE & LCD_OSDS_EOF1 )
#define __lcd_start_of_fg0()		( REG_LCD_STATE & LCD_OSDS_SOF0 )
#define __lcd_end_of_fg0()		( REG_LCD_STATE & LCD_OSDS_EOF0 )
#define __lcd_change_is_rdy()		( REG_LCD_STATE & LCD_OSDS_READY )

/* Foreground Color Key Register 0,1(foreground 0, foreground 1) */
#define __lcd_enable_colorkey0()	(REG_LCD_KEY0 |= LCD_KEY_KEYEN)
#define __lcd_enable_colorkey1()	(REG_LCD_KEY1 |= LCD_KEY_KEYEN)
#define __lcd_enable_colorkey0_md() 	(REG_LCD_KEY0 |= LCD_KEY_KEYMD)
#define __lcd_enable_colorkey1_md() 	(REG_LCD_KEY1 |= LCD_KEY_KEYMD)
#define __lcd_set_colorkey0(key) 	(REG_LCD_KEY0 = (REG_LCD_KEY0&~0xFFFFFF)|(key))
#define __lcd_set_colorkey1(key) 	(REG_LCD_KEY1 = (REG_LCD_KEY1&~0xFFFFFF)|(key))

#define __lcd_disable_colorkey0() 	(REG_LCD_KEY0 &= ~LCD_KEY_KEYEN)
#define __lcd_disable_colorkey1() 	(REG_LCD_KEY1 &= ~LCD_KEY_KEYEN)
#define __lcd_disable_colorkey0_md() 	(REG_LCD_KEY0 &= ~LCD_KEY_KEYMD)
#define __lcd_disable_colorkey1_md() 	(REG_LCD_KEY1 &= ~LCD_KEY_KEYMD)

/* IPU Restart Register */
#define __lcd_enable_ipu_restart() 	(REG_LCD_IPUR |= LCD_IPUR_IPUREN)
#define __lcd_disable_ipu_restart() 	(REG_LCD_IPUR &= ~LCD_IPUR_IPUREN)
#define __lcd_set_ipu_restart_triger(n)	(REG_LCD_IPUR = (REG_LCD_IPUR&(~0xFFFFFF))|(n))

/* RGB Control Register */
#define __lcd_enable_rgb_dummy() 	(REG_LCD_RGBC |= LCD_RGBC_RGBDM)
#define __lcd_disable_rgb_dummy() 	(REG_LCD_RGBC &= ~LCD_RGBC_RGBDM)

#define __lcd_dummy_rgb() 	(REG_LCD_RGBC |= LCD_RGBC_DMM)
#define __lcd_rgb_dummy() 	(REG_LCD_RGBC &= ~LCD_RGBC_DMM)

#define __lcd_rgb2ycc() 	(REG_LCD_RGBC |= LCD_RGBC_YCC)
#define __lcd_notrgb2ycc() 	(REG_LCD_RGBC &= ~LCD_RGBC_YCC)

#define __lcd_odd_mode_rgb() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_RGB )
#define __lcd_odd_mode_rbg() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_RBG )
#define __lcd_odd_mode_grb() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_GRB)

#define __lcd_odd_mode_gbr() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_GBR)
#define __lcd_odd_mode_brg() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_BRG)
#define __lcd_odd_mode_bgr() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_ODDRGB_MASK) | LCD_RGBC_ODD_BGR)

#define __lcd_even_mode_rgb() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_RGB )
#define __lcd_even_mode_rbg() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_RBG )
#define __lcd_even_mode_grb() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_GRB)

#define __lcd_even_mode_gbr() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_GBR)
#define __lcd_even_mode_brg() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_BRG)
#define __lcd_even_mode_bgr() \
  ( REG_LCD_RGBC = (REG_LCD_RGBC & ~LCD_RGBC_EVENRGB_MASK) | LCD_RGBC_EVEN_BGR)

/* Vertical Synchronize Register */
#define __lcd_vsync_get_vps() \
  ( (REG_LCD_VSYNC & LCD_VSYNC_VPS_MASK) >> LCD_VSYNC_VPS_BIT )

#define __lcd_vsync_get_vpe() \
  ( (REG_LCD_VSYNC & LCD_VSYNC_VPE_MASK) >> LCD_VSYNC_VPE_BIT )
#define __lcd_vsync_set_vpe(n) 				\
do {							\
	REG_LCD_VSYNC &= ~LCD_VSYNC_VPE_MASK;		\
	REG_LCD_VSYNC |= (n) << LCD_VSYNC_VPE_BIT;	\
} while (0)

#define __lcd_hsync_get_hps() \
  ( (REG_LCD_HSYNC & LCD_HSYNC_HPS_MASK) >> LCD_HSYNC_HPS_BIT )
#define __lcd_hsync_set_hps(n) 				\
do {							\
	REG_LCD_HSYNC &= ~LCD_HSYNC_HPS_MASK;		\
	REG_LCD_HSYNC |= (n) << LCD_HSYNC_HPS_BIT;	\
} while (0)

#define __lcd_hsync_get_hpe() \
  ( (REG_LCD_HSYNC & LCD_HSYNC_HPE_MASK) >> LCD_VSYNC_HPE_BIT )
#define __lcd_hsync_set_hpe(n) 				\
do {							\
	REG_LCD_HSYNC &= ~LCD_HSYNC_HPE_MASK;		\
	REG_LCD_HSYNC |= (n) << LCD_HSYNC_HPE_BIT;	\
} while (0)

#define __lcd_vat_get_ht() \
  ( (REG_LCD_VAT & LCD_VAT_HT_MASK) >> LCD_VAT_HT_BIT )
#define __lcd_vat_set_ht(n) 				\
do {							\
	REG_LCD_VAT &= ~LCD_VAT_HT_MASK;		\
	REG_LCD_VAT |= (n) << LCD_VAT_HT_BIT;		\
} while (0)

#define __lcd_vat_get_vt() \
  ( (REG_LCD_VAT & LCD_VAT_VT_MASK) >> LCD_VAT_VT_BIT )
#define __lcd_vat_set_vt(n) 				\
do {							\
	REG_LCD_VAT &= ~LCD_VAT_VT_MASK;		\
	REG_LCD_VAT |= (n) << LCD_VAT_VT_BIT;		\
} while (0)

#define __lcd_dah_get_hds() \
  ( (REG_LCD_DAH & LCD_DAH_HDS_MASK) >> LCD_DAH_HDS_BIT )
#define __lcd_dah_set_hds(n) 				\
do {							\
	REG_LCD_DAH &= ~LCD_DAH_HDS_MASK;		\
	REG_LCD_DAH |= (n) << LCD_DAH_HDS_BIT;		\
} while (0)

#define __lcd_dah_get_hde() \
  ( (REG_LCD_DAH & LCD_DAH_HDE_MASK) >> LCD_DAH_HDE_BIT )
#define __lcd_dah_set_hde(n) 				\
do {							\
	REG_LCD_DAH &= ~LCD_DAH_HDE_MASK;		\
	REG_LCD_DAH |= (n) << LCD_DAH_HDE_BIT;		\
} while (0)

#define __lcd_dav_get_vds() \
  ( (REG_LCD_DAV & LCD_DAV_VDS_MASK) >> LCD_DAV_VDS_BIT )
#define __lcd_dav_set_vds(n) 				\
do {							\
	REG_LCD_DAV &= ~LCD_DAV_VDS_MASK;		\
	REG_LCD_DAV |= (n) << LCD_DAV_VDS_BIT;		\
} while (0)

#define __lcd_dav_get_vde() \
  ( (REG_LCD_DAV & LCD_DAV_VDE_MASK) >> LCD_DAV_VDE_BIT )
#define __lcd_dav_set_vde(n) 				\
do {							\
	REG_LCD_DAV &= ~LCD_DAV_VDE_MASK;		\
	REG_LCD_DAV |= (n) << LCD_DAV_VDE_BIT;		\
} while (0)

/* DMA Command Register */
#define __lcd_cmd0_set_sofint()		( REG_LCD_CMD0 |= LCD_CMD_SOFINT )
#define __lcd_cmd0_clr_sofint()		( REG_LCD_CMD0 &= ~LCD_CMD_SOFINT )
#define __lcd_cmd1_set_sofint()		( REG_LCD_CMD1 |= LCD_CMD_SOFINT )
#define __lcd_cmd1_clr_sofint()		( REG_LCD_CMD1 &= ~LCD_CMD_SOFINT )

#define __lcd_cmd0_set_eofint()		( REG_LCD_CMD0 |= LCD_CMD_EOFINT )
#define __lcd_cmd0_clr_eofint()		( REG_LCD_CMD0 &= ~LCD_CMD_EOFINT )
#define __lcd_cmd1_set_eofint()		( REG_LCD_CMD1 |= LCD_CMD_EOFINT )
#define __lcd_cmd1_clr_eofint()		( REG_LCD_CMD1 &= ~LCD_CMD_EOFINT )

#define __lcd_cmd0_set_pal()		( REG_LCD_CMD0 |= LCD_CMD_PAL )
#define __lcd_cmd0_clr_pal()		( REG_LCD_CMD0 &= ~LCD_CMD_PAL )

#define __lcd_cmd0_get_len() \
  ( (REG_LCD_CMD0 & LCD_CMD_LEN_MASK) >> LCD_CMD_LEN_BIT )
#define __lcd_cmd1_get_len() \
  ( (REG_LCD_CMD1 & LCD_CMD_LEN_MASK) >> LCD_CMD_LEN_BIT )

#endif /* __MIPS_ASSEMBLER */

/*
 * Motion compensation module(MC) address definition
 */
#define	MC_BASE		0xb3250000

/*
 * MC registers offset address definition
 */
#define MC_MCCR_OFFSET		(0x00)	/* rw, 32, 0x???????? */
#define MC_MCSR_OFFSET		(0x04)	/* rw, 32, 0x???????? */
#define MC_MCRBAR_OFFSET	(0x08)	/* rw, 32, 0x???????? */
#define MC_MCT1LFCR_OFFSET	(0x0c)	/* rw, 32, 0x???????? */
#define MC_MCT2LFCR_OFFSET	(0x10)	/* rw, 32, 0x???????? */
#define MC_MCCBAR_OFFSET	(0x14)	/* rw, 32, 0x???????? */
#define MC_MCIIR_OFFSET		(0x18)	/* rw, 32, 0x???????? */
#define MC_MCSIR_OFFSET		(0x1c)	/* rw, 32, 0x???????? */
#define MC_MCT1MFCR_OFFSET	(0x20)	/* rw, 32, 0x???????? */
#define MC_MCT2MFCR_OFFSET	(0x24)	/* rw, 32, 0x???????? */
#define MC_MCFGIR_OFFSET	(0x28)	/* rw, 32, 0x???????? */
#define MC_MCFCIR_OFFSET	(0x2c)	/* rw, 32, 0x???????? */
#define MC_MCRNDTR_OFFSET	(0x40)	/* rw, 32, 0x???????? */

#define MC_MC2CR_OFFSET		(0x8000)	/* rw, 32, 0x???????? */
#define MC_MC2SR_OFFSET		(0x8004)	/* rw, 32, 0x???????? */
#define MC_MC2RBAR_OFFSET	(0x8008)	/* rw, 32, 0x???????? */
#define MC_MC2CBAR_OFFSET	(0x800c)	/* rw, 32, 0x???????? */
#define MC_MC2IIR_OFFSET	(0x8010)	/* rw, 32, 0x???????? */
#define MC_MC2TFCR_OFFSET	(0x8014)	/* rw, 32, 0x???????? */
#define MC_MC2SIR_OFFSET	(0x8018)	/* rw, 32, 0x???????? */
#define MC_MC2FCIR_OFFSET	(0x801c)	/* rw, 32, 0x???????? */
#define MC_MC2RNDTR_OFFSET	(0x8040)	/* rw, 32, 0x???????? */

/*
 * MC registers address definition
 */
#define MC_MCCR		(MC_BASE + MC_MCCR_OFFSET)
#define MC_MCSR		(MC_BASE + MC_MCSR_OFFSET)
#define MC_MCRBAR	(MC_BASE + MC_MCRBAR_OFFSET)
#define MC_MCT1LFCR	(MC_BASE + MC_MCT1LFCR_OFFSET)
#define MC_MCT2LFCR	(MC_BASE + MC_MCT2LFCR_OFFSET)
#define MC_MCCBAR	(MC_BASE + MC_MCCBAR_OFFSET)
#define MC_MCIIR	(MC_BASE + MC_MCIIR_OFFSET)
#define MC_MCSIR	(MC_BASE + MC_MCSIR_OFFSET)
#define MC_MCT1MFCR	(MC_BASE + MC_MCT1MFCR_OFFSET)
#define MC_MCT2MFCR	(MC_BASE + MC_MCT2MFCR_OFFSET)
#define MC_MCFGIR	(MC_BASE + MC_MCFGIR_OFFSET)
#define MC_MCFCIR	(MC_BASE + MC_MCFCIR_OFFSET)
#define MC_MCRNDTR	(MC_BASE + MC_MCRNDTR_OFFSET)

#define MC_MC2CR	(MC_BASE + MC_MC2CR_OFFSET)
#define MC_MC2SR	(MC_BASE + MC_MC2SR_OFFSET)
#define MC_MC2RBAR	(MC_BASE + MC_MC2RBAR_OFFSET)
#define MC_MC2CBAR	(MC_BASE + MC_MC2CBAR_OFFSET)
#define MC_MC2IIR	(MC_BASE + MC_MC2IIR_OFFSET)
#define MC_MC2TFCR	(MC_BASE + MC_MC2TFCR_OFFSET)
#define MC_MC2SIR	(MC_BASE + MC_MC2SIR_OFFSET)
#define MC_MC2FCIR	(MC_BASE + MC_MC2FCIR_OFFSET)
#define MC_MC2RNDTR	(MC_BASE + MC_MC2RNDTR_OFFSET)

/*
 * MC registers common define
 */

/* MC Control Register(MCCR) */
#define MCCR_RETE		BIT16
#define MCCR_DIPE		BIT7
#define MCCR_CKGEN		BIT6
#define MCCR_FDDEN		BIT5
#define MCCR_DINSE		BIT3
#define MCCR_FAE		BIT2
#define MCCR_RST		BIT1
#define MCCR_CHEN		BIT0

#define MCCR_FDDPGN_LSB		8
#define MCCR_FDDPGN_MASK	BITS_H2L(15, MCCR_FDDPGN_LSB)

/* MC Status Register(MCSR) */
#define MCSR_DLEND		BIT1
#define MCSR_BKLEND		BIT0

#ifndef __MIPS_ASSEMBLER

#define REG_MC_MCCR		REG32(REG_MC_MCCR)
#define REG_MC_MCSR             REG32(REG_MC_MCSR)
#define REG_MC_MCRBAR           REG32(REG_MC_MCRBAR)
#define REG_MC_MCT1LFCR         REG32(REG_MC_MCT1LFCR)
#define REG_MC_MCT2LFCR         REG32(REG_MC_MCT2LFCR)
#define REG_MC_MCCBAR           REG32(REG_MC_MCCBAR)
#define REG_MC_MCIIR            REG32(REG_MC_MCIIR)
#define REG_MC_MCSIR            REG32(REG_MC_MCSIR)
#define REG_MC_MCT1MFCR         REG32(REG_MC_MCT1MFCR)
#define REG_MC_MCT2MFCR         REG32(REG_MC_MCT2MFCR)
#define REG_MC_MCFGIR           REG32(REG_MC_MCFGIR)
#define REG_MC_MCFCIR           REG32(REG_MC_MCFCIR)
#define REG_MC_MCRNDTR          REG32(REG_MC_MCRNDTR)

#define REG_MC_MC2CR            REG32(REG_MC_MC2CR)
#define REG_MC_MC2SR            REG32(REG_MC_MC2SR)
#define REG_MC_MC2RBAR          REG32(REG_MC_MC2RBAR)
#define REG_MC_MC2CBAR          REG32(REG_MC_MC2CBAR)
#define REG_MC_MC2IIR           REG32(REG_MC_MC2IIR)
#define REG_MC_MC2TFCR          REG32(REG_MC_MC2TFCR)
#define REG_MC_MC2SIR           REG32(REG_MC_MC2SIR)
#define REG_MC_MC2FCIR          REG32(REG_MC_MC2FCIR)
#define REG_MC_MC2RNDTR         REG32(REG_MC_MC2RNDTR)

#endif /* __MIPS_ASSEMBLER */

#define	MDMAC_BASE	0xB3030000 /* Memory Copy DMAC */

/*************************************************************************
 * MDMAC (MEM Copy DMA Controller)
 *************************************************************************/

/* m is the DMA controller index (0, 1), n is the DMA channel index (0 - 11) */

#define MDMAC_DSAR(n)		(MDMAC_BASE + (0x00 + (n) * 0x20)) /* DMA source address */
#define MDMAC_DTAR(n)  		(MDMAC_BASE + (0x04 + (n) * 0x20)) /* DMA target address */
#define MDMAC_DTCR(n)  		(MDMAC_BASE + (0x08 + (n) * 0x20)) /* DMA transfer count */
#define MDMAC_DRSR(n)  		(MDMAC_BASE + (0x0c + (n) * 0x20)) /* DMA request source */
#define MDMAC_DCCSR(n) 		(MDMAC_BASE + (0x10 + (n) * 0x20)) /* DMA control/status */
#define MDMAC_DCMD(n)  		(MDMAC_BASE + (0x14 + (n) * 0x20)) /* DMA command */
#define MDMAC_DDA(n)   		(MDMAC_BASE + (0x18 + (n) * 0x20)) /* DMA descriptor address */
#define MDMAC_DSD(n)   		(MDMAC_BASE + (0x1c + (n) * 0x20)) /* DMA Stride Address */

#define MDMAC_DMACR		(MDMAC_BASE + 0x0300) /* DMA control register */
#define MDMAC_DMAIPR		(MDMAC_BASE + 0x0304) /* DMA interrupt pending */
#define MDMAC_DMADBR		(MDMAC_BASE + 0x0308) /* DMA doorbell */
#define MDMAC_DMADBSR		(MDMAC_BASE + 0x030C) /* DMA doorbell set */
#define MDMAC_DMACKE  		(MDMAC_BASE + 0x0310)
#define MDMAC_DMACKES  		(MDMAC_BASE + 0x0314)
#define MDMAC_DMACKEC  		(MDMAC_BASE + 0x0318)

#define REG_MDMAC_DSAR(n)	REG32(MDMAC_DSAR((n)))
#define REG_MDMAC_DTAR(n)	REG32(MDMAC_DTAR((n)))
#define REG_MDMAC_DTCR(n)	REG32(MDMAC_DTCR((n)))
#define REG_MDMAC_DRSR(n)	REG32(MDMAC_DRSR((n)))
#define REG_MDMAC_DCCSR(n)	REG32(MDMAC_DCCSR((n)))
#define REG_MDMAC_DCMD(n)	REG32(MDMAC_DCMD((n)))
#define REG_MDMAC_DDA(n)	REG32(MDMAC_DDA((n)))
#define REG_MDMAC_DSD(n)        REG32(MDMAC_DSD(n))
#define REG_MDMAC_DMACR		REG32(MDMAC_DMACR)
#define REG_MDMAC_DMAIPR	REG32(MDMAC_DMAIPR)
#define REG_MDMAC_DMADBR	REG32(MDMAC_DMADBR)
#define REG_MDMAC_DMADBSR	REG32(MDMAC_DMADBSR)
#define REG_MDMAC_DMACKE     	REG32(MDMAC_DMACKE)
#define REG_MDMAC_DMACKES     	REG32(MDMAC_DMACKES)
#define REG_MDMAC_DMACKEC     	REG32(MDMAC_DMACKEC)

// DMA control register
#define DMAC_MDMACR_HLT		(1 << 3)  /* DMA halt flag */
#define DMAC_MDMACR_AR		(1 << 2)  /* address error flag */
#define DMAC_MDMACR_DMAE	(1 << 0)  /* DMA enable bit */

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * Mem Copy DMAC
 ***************************************************************************/

#define __mdmac_enable_module() \
	( REG_MDMAC_DMACR |= DMAC_MDMACR_DMAE )
#define __mdmac_disable_module() \
	( REG_MDMAC_DMACR &= ~DMAC_MDMACR_DMAE )

#define __mdmac_test_halt_error ( REG_MDMAC_DMACR & DMAC_MDMACR_HLT )
#define __mdmac_test_addr_error ( REG_MDMAC_DMACR & DMAC_MDMACR_AR )

#define __mdmac_channel_enable_clk \
	REG_MDMAC_DMACKES = 1 << (n);

#define __mdmac_channel_disable_clk \
	REG_MDMAC_DMACKEC = 1 << (n);

#define __mdmac_enable_descriptor(n) \
  ( REG_MDMAC_DCCSR((n)) &= ~DMAC_DCCSR_NDES )
#define __mdmac_disable_descriptor(n) \
  ( REG_MDMAC_DCCSR((n)) |= DMAC_DCCSR_NDES )

#define __mdmac_enable_channel(n)                 \
do {                                             \
	REG_MDMAC_DCCSR((n)) |= DMAC_DCCSR_EN;    \
} while (0)
#define __mdmac_disable_channel(n)                \
do {                                             \
	REG_MDMAC_DCCSR((n)) &= ~DMAC_DCCSR_EN;   \
} while (0)
#define __mdmac_channel_enabled(n) \
  ( REG_MDMAC_DCCSR((n)) & DMAC_DCCSR_EN )

#define __mdmac_channel_enable_irq(n) \
  ( REG_MDMAC_DCMD((n)) |= DMAC_DCMD_TIE )
#define __mdmac_channel_disable_irq(n) \
  ( REG_DMAC_DCMD((n)) &= ~DMAC_DCMD_TIE )

#define __mdmac_channel_transmit_halt_detected(n) \
  (  REG_MDMAC_DCCSR((n)) & DMAC_DCCSR_HLT )
#define __mdmac_channel_transmit_end_detected(n) \
  (  REG_MDMAC_DCCSR((n)) & DMAC_DCCSR_TT )
#define __mdmac_channel_address_error_detected(n) \
  (  REG_DMAC_DCCSR((n)) & DMAC_DCCSR_AR )
#define __mdmac_channel_count_terminated_detected(n) \
  (  REG_MDMAC_DCCSR((n)) & DMAC_DCCSR_CT )
#define __mdmac_channel_descriptor_invalid_detected(n) \
  (  REG_MDMAC_DCCSR((n)) & DMAC_DCCSR_INV )

#define __mdmac_channel_clear_transmit_halt(n)				\
	do {								\
		/* clear both channel halt error and globle halt error */ \
		REG_MDMAC_DCCSR(n) &= ~DMAC_DCCSR_HLT;			\
		REG_MDMAC_DMACR &= ~DMAC_DMACR_HLT;	\
	} while (0)
#define __mdmac_channel_clear_transmit_end(n) \
  (  REG_MDMAC_DCCSR(n) &= ~DMAC_DCCSR_TT )
#define __mdmac_channel_clear_address_error(n)				\
	do {								\
		REG_MDMAC_DDA(n) = 0; /* clear descriptor address register */ \
		REG_MDMAC_DSAR(n) = 0; /* clear source address register */ \
		REG_MDMAC_DTAR(n) = 0; /* clear target address register */ \
		/* clear both channel addr error and globle address error */ \
		REG_MDMAC_DCCSR(n) &= ~DMAC_DCCSR_AR;			\
		REG_MDMAC_DMACR &= ~DMAC_DMACR_AR;	\
	} while (0)
#define __mdmac_channel_clear_count_terminated(n) \
  (  REG_MDMAC_DCCSR((n)) &= ~DMAC_DCCSR_CT )
#define __mdmac_channel_clear_descriptor_invalid(n) \
  (  REG_MDMAC_DCCSR((n)) &= ~DMAC_DCCSR_INV )

#define __mdmac_channel_set_transfer_unit_32bit(n)	\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DS_32BIT;	\
} while (0)

#define __mdmac_channel_set_transfer_unit_16bit(n)	\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DS_16BIT;	\
} while (0)

#define __mdmac_channel_set_transfer_unit_8bit(n)	\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DS_8BIT;	\
} while (0)

#define __mdmac_channel_set_transfer_unit_16byte(n)	\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DS_16BYTE;	\
} while (0)

#define __mdmac_channel_set_transfer_unit_32byte(n)	\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DS_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DS_32BYTE;	\
} while (0)

/* w=8,16,32 */
#define __mdmac_channel_set_dest_port_width(n,w)		\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DWDH_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DWDH_##w;	\
} while (0)

/* w=8,16,32 */
#define __mdmac_channel_set_src_port_width(n,w)		\
do {							\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_SWDH_MASK;	\
	REG_MDMAC_DCMD((n)) |= DMAC_DCMD_SWDH_##w;	\
} while (0)

/* v=0-15 */
#define __mdmac_channel_set_rdil(n,v)				\
do {								\
	REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_RDIL_MASK;		\
	REG_MDMAC_DCMD((n) |= ((v) << DMAC_DCMD_RDIL_BIT);	\
} while (0)

#define __mdmac_channel_dest_addr_fixed(n) \
	(REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_DAI)
#define __mdmac_channel_dest_addr_increment(n) \
	(REG_MDMAC_DCMD((n)) |= DMAC_DCMD_DAI)

#define __mdmac_channel_src_addr_fixed(n) \
	(REG_MDMAC_DCMD((n)) &= ~DMAC_DCMD_SAI)
#define __mdmac_channel_src_addr_increment(n) \
	(REG_MDMAC_DCMD((n)) |= DMAC_DCMD_SAI)

#define __mdmac_channel_set_doorbell(n)	\
	(REG_MDMAC_DMADBSR = (1 << (n)))

#define __mdmac_channel_irq_detected(n)  (REG_MDMAC_DMAIPR & (1 << (n)))
#define __mdmac_channel_ack_irq(n)       (REG_MDMAC_DMAIPR &= ~(1 <<(n)))

static __inline__ int __mdmac_get_irq(void)
{
	int i;
	for (i = 0; i < MAX_MDMA_NUM; i++)
		if (__mdmac_channel_irq_detected(i))
			return i;
	return -1;
}

#endif /* __MIPS_ASSEMBLER */

/*
 * Motion estimation module(ME) address definition
 */
#define	ME_BASE		0xb3260000

/*
 * ME registers offset address definition
 */
#define ME_MECR_OFFSET		(0x00)  /* rw, 32, 0x???????0 */
#define ME_MERBAR_OFFSET	(0x04)  /* rw, 32, 0x???????? */
#define ME_MECBAR_OFFSET	(0x08)  /* rw, 32, 0x???????? */
#define ME_MEDAR_OFFSET		(0x0c)  /* rw, 32, 0x???????? */
#define ME_MERFSR_OFFSET	(0x10)  /* rw, 32, 0x???????? */
#define ME_MECFSR_OFFSET	(0x14)  /* rw, 32, 0x???????? */
#define ME_MEDFSR_OFFSET	(0x18)  /* rw, 32, 0x???????? */
#define ME_MESR_OFFSET		(0x1c)  /* rw, 32, 0x???????? */
#define ME_MEMR_OFFSET		(0x20)  /* rw, 32, 0x???????? */
#define ME_MEFR_OFFSET		(0x24)  /* rw, 32, 0x???????? */

/*
 * ME registers address definition
 */
#define ME_MECR		(ME_BASE + ME_MECR_OFFSET)
#define ME_MERBAR	(ME_BASE + ME_MERBAR_OFFSET)
#define ME_MECBAR       (ME_BASE + ME_MECBAR_OFFSET)
#define ME_MEDAR        (ME_BASE + ME_MEDAR_OFFSET)
#define ME_MERFSR       (ME_BASE + ME_MERFSR_OFFSET)
#define ME_MECFSR       (ME_BASE + ME_MECFSR_OFFSET)
#define ME_MEDFSR       (ME_BASE + ME_MEDFSR_OFFSET)
#define ME_MESR         (ME_BASE + ME_MESR_OFFSET)
#define ME_MEMR         (ME_BASE + ME_MEMR_OFFSET)
#define ME_MEFR         (ME_BASE + ME_MEFR_OFFSET)

/*
 * ME registers common define
 */

/* ME control register(MECR) */
#define MECR_FLUSH		BIT2
#define MECR_RESET		BIT1
#define MECR_ENABLE		BIT0

/* ME settings register(MESR) */
#define MESR_GATE_LSB		16
#define MESR_GATE_MASK		BITS_H2L(31, MESR_GATE_LSB)

#define MESR_NUM_LSB		0
#define MESR_NUM_MASK		BITS_H2L(5, MESR_NUM_LSB)

/* ME MVD register(MEMR) */
#define MEMR_MVDY_LSB		16
#define MESR_MVDY_MASK		BITS_H2L(31, MEMR_MVDY_LSB)

#define MEMR_MVDX_LSB		0
#define MESR_MVDX_MASK		BITS_H2L(15, MEMR_MVDX_LSB)

/* ME flag register(MEFR) */
#define MEFR_INTRA		BIT1
#define MEFR_COMPLETED		BIT0

#ifndef __MIPS_ASSEMBLER

#define REG_ME_MECR	REG32(ME_MECR)
#define REG_ME_MERBAR	REG32(ME_MERBAR)
#define REG_ME_MECBAR	REG32(ME_MECBAR)
#define REG_ME_MEDAR	REG32(ME_MEDAR)
#define REG_ME_MERFSR	REG32(ME_MERFSR)
#define REG_ME_MECFSR	REG32(ME_MECFSR)
#define REG_ME_MEDFSR	REG32(ME_MEDFSR)
#define REG_ME_MESR	REG32(ME_MESR)
#define REG_ME_MEMR	REG32(ME_MEMR)
#define REG_ME_MEFR	REG32(ME_MEFR)

#endif /* __MIPS_ASSEMBLER */

#define	MSC0_BASE	0xB0021000
#define	MSC1_BASE	0xB0022000
#define	MSC2_BASE	0xB0023000

/*************************************************************************
 * MSC
 ************************************************************************/
/* n = 0, 1 (MSC0, MSC1) */
#define	MSC_STRPCL(n)		(MSC0_BASE + (n)*0x1000 + 0x000)
#define	MSC_STAT(n)		(MSC0_BASE + (n)*0x1000 + 0x004)
#define	MSC_CLKRT(n)		(MSC0_BASE + (n)*0x1000 + 0x008)
#define	MSC_CMDAT(n)		(MSC0_BASE + (n)*0x1000 + 0x00C)
#define	MSC_RESTO(n)		(MSC0_BASE + (n)*0x1000 + 0x010)
#define	MSC_RDTO(n)		(MSC0_BASE + (n)*0x1000 + 0x014)
#define	MSC_BLKLEN(n)		(MSC0_BASE + (n)*0x1000 + 0x018)
#define	MSC_NOB(n)		(MSC0_BASE + (n)*0x1000 + 0x01C)
#define	MSC_SNOB(n)		(MSC0_BASE + (n)*0x1000 + 0x020)
#define	MSC_IMASK(n)		(MSC0_BASE + (n)*0x1000 + 0x024)
#define	MSC_IREG(n)		(MSC0_BASE + (n)*0x1000 + 0x028)
#define	MSC_CMD(n)		(MSC0_BASE + (n)*0x1000 + 0x02C)
#define	MSC_ARG(n)		(MSC0_BASE + (n)*0x1000 + 0x030)
#define	MSC_RES(n)		(MSC0_BASE + (n)*0x1000 + 0x034)
#define	MSC_RXFIFO(n)		(MSC0_BASE + (n)*0x1000 + 0x038)
#define	MSC_TXFIFO(n)		(MSC0_BASE + (n)*0x1000 + 0x03C)
#define	MSC_LPM(n)		(MSC0_BASE + (n)*0x1000 + 0x040)

#define	REG_MSC_STRPCL(n)	REG16(MSC_STRPCL(n))
#define	REG_MSC_STAT(n)		REG32(MSC_STAT(n))
#define	REG_MSC_CLKRT(n)	REG16(MSC_CLKRT(n))
#define	REG_MSC_CMDAT(n)	REG32(MSC_CMDAT(n))
#define	REG_MSC_RESTO(n)	REG16(MSC_RESTO(n))
#define	REG_MSC_RDTO(n)		REG32(MSC_RDTO(n))
#define	REG_MSC_BLKLEN(n)	REG16(MSC_BLKLEN(n))
#define	REG_MSC_NOB(n)		REG16(MSC_NOB(n))
#define	REG_MSC_SNOB(n)		REG16(MSC_SNOB(n))
#define	REG_MSC_IMASK(n)	REG32(MSC_IMASK(n))
#define	REG_MSC_IREG(n)		REG16(MSC_IREG(n))
#define	REG_MSC_CMD(n)		REG8(MSC_CMD(n))
#define	REG_MSC_ARG(n)		REG32(MSC_ARG(n))
#define	REG_MSC_RES(n)		REG16(MSC_RES(n))
#define	REG_MSC_RXFIFO(n)	REG32(MSC_RXFIFO(n))
#define	REG_MSC_TXFIFO(n)	REG32(MSC_TXFIFO(n))
#define	REG_MSC_LPM(n)		REG32(MSC_LPM(n))

/* MSC Clock and Control Register (MSC_STRPCL) */
#define MSC_STRPCL_SEND_CCSD		(1 << 15) /*send command completion signal disable to ceata */
#define MSC_STRPCL_SEND_AS_CCSD		(1 << 14) /*send internally generated stop after sending ccsd */
#define MSC_STRPCL_EXIT_MULTIPLE	(1 << 7)
#define MSC_STRPCL_EXIT_TRANSFER	(1 << 6)
#define MSC_STRPCL_START_READWAIT	(1 << 5)
#define MSC_STRPCL_STOP_READWAIT	(1 << 4)
#define MSC_STRPCL_RESET		(1 << 3)
#define MSC_STRPCL_START_OP		(1 << 2)
#define MSC_STRPCL_CLOCK_CONTROL_BIT	0
#define MSC_STRPCL_CLOCK_CONTROL_MASK	(0x3 << MSC_STRPCL_CLOCK_CONTROL_BIT)
  #define MSC_STRPCL_CLOCK_CONTROL_STOP	  (0x1 << MSC_STRPCL_CLOCK_CONTROL_BIT) /* Stop MMC/SD clock */
  #define MSC_STRPCL_CLOCK_CONTROL_START  (0x2 << MSC_STRPCL_CLOCK_CONTROL_BIT) /* Start MMC/SD clock */

/* MSC Status Register (MSC_STAT) */
#define MSC_STAT_AUTO_CMD_DONE		(1 << 31) /*12 is internally generated by controller has finished */
#define MSC_STAT_IS_RESETTING		(1 << 15)
#define MSC_STAT_SDIO_INT_ACTIVE	(1 << 14)
#define MSC_STAT_PRG_DONE		(1 << 13)
#define MSC_STAT_DATA_TRAN_DONE		(1 << 12)
#define MSC_STAT_END_CMD_RES		(1 << 11)
#define MSC_STAT_DATA_FIFO_AFULL	(1 << 10)
#define MSC_STAT_IS_READWAIT		(1 << 9)
#define MSC_STAT_CLK_EN			(1 << 8)
#define MSC_STAT_DATA_FIFO_FULL		(1 << 7)
#define MSC_STAT_DATA_FIFO_EMPTY	(1 << 6)
#define MSC_STAT_CRC_RES_ERR		(1 << 5)
#define MSC_STAT_CRC_READ_ERROR		(1 << 4)
#define MSC_STAT_CRC_WRITE_ERROR_BIT	2
#define MSC_STAT_CRC_WRITE_ERROR_MASK	(0x3 << MSC_STAT_CRC_WRITE_ERROR_BIT)
  #define MSC_STAT_CRC_WRITE_ERROR_NO		(0 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* No error on transmission of data */
  #define MSC_STAT_CRC_WRITE_ERROR		(1 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* Card observed erroneous transmission of data */
  #define MSC_STAT_CRC_WRITE_ERROR_NOSTS	(2 << MSC_STAT_CRC_WRITE_ERROR_BIT) /* No CRC status is sent back */
#define MSC_STAT_TIME_OUT_RES		(1 << 1)
#define MSC_STAT_TIME_OUT_READ		(1 << 0)

/* MSC Bus Clock Control Register (MSC_CLKRT) */
#define	MSC_CLKRT_CLK_DIV_BIT		14
#define	MSC_CLKRT_CLK_DIV_MASK		(0x3 << MSC_CLKRT_CLK_DIV_BIT)
#define MSC_CLKRT_CLK_SRC_DIV_1		(0x0 << MSC_CLKRT_CLK_DIV_BIT) /* CLK_SRC */
#define MSC_CLKRT_CLK_SRC_DIV_2		(0x1 << MSC_CLKRT_CLK_DIV_BIT) /* 1/2 of CLK_SRC */
#define MSC_CLKRT_CLK_SRC_DIV_3		(0x2 << MSC_CLKRT_CLK_DIV_BIT) /* 1/3 of CLK_SRC */
#define MSC_CLKRT_CLK_SRC_DIV_4		(0x3 << MSC_CLKRT_CLK_DIV_BIT) /* 1/4 of CLK_SRC */

#define	MSC_CLKRT_CLK_RATE_BIT		0
#define	MSC_CLKRT_CLK_RATE_MASK		(0x7 << MSC_CLKRT_CLK_RATE_BIT)
#define MSC_CLKRT_CLK_RATE_DIV_1	(0x0 << MSC_CLKRT_CLK_RATE_BIT) /* CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_2	(0x1 << MSC_CLKRT_CLK_RATE_BIT) /* 1/2 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_4	(0x2 << MSC_CLKRT_CLK_RATE_BIT) /* 1/4 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_8	(0x3 << MSC_CLKRT_CLK_RATE_BIT) /* 1/8 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_16	(0x4 << MSC_CLKRT_CLK_RATE_BIT) /* 1/16 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_32	(0x5 << MSC_CLKRT_CLK_RATE_BIT) /* 1/32 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_64	(0x6 << MSC_CLKRT_CLK_RATE_BIT) /* 1/64 of CLK_SRC */
#define MSC_CLKRT_CLK_RATE_DIV_128	(0x7 << MSC_CLKRT_CLK_RATE_BIT) /* 1/128 of CLK_SRC */

/* MSC Command Sequence Control Register (MSC_CMDAT) */
#define	MSC_CMDAT_CCS_EXPECTED		(1 << 31) /* interrupts are enabled in ce-ata */
#define	MSC_CMDAT_READ_CEATA		(1 << 30)
#define	MSC_CMDAT_SDIO_PRDT		(1 << 17) /* exact 2 cycle */
#define	MSC_CMDAT_SEND_AS_STOP		(1 << 16)
#define	MSC_CMDAT_RTRG_BIT		14
#define MSC_CMDAT_RTRG_EQUALT_8	        (0x0 << MSC_CMDAT_RTRG_BIT) /*reset value*/
  #define MSC_CMDAT_RTRG_EQUALT_16	(0x1 << MSC_CMDAT_RTRG_BIT)
  #define MSC_CMDAT_RTRG_EQUALT_24	(0x2 << MSC_CMDAT_RTRG_BIT)

#define	MSC_CMDAT_TTRG_BIT		12
#define MSC_CMDAT_TTRG_LESS_8		(0x0 << MSC_CMDAT_TTRG_BIT) /*reset value*/
  #define MSC_CMDAT_TTRG_LESS_16	(0x1 << MSC_CMDAT_TTRG_BIT)
  #define MSC_CMDAT_TTRG_LESS_24	(0x2 << MSC_CMDAT_TTRG_BIT)
#define	MSC_CMDAT_STOP_ABORT		(1 << 11)
#define	MSC_CMDAT_BUS_WIDTH_BIT		9
#define	MSC_CMDAT_BUS_WIDTH_MASK	(0x3 << MSC_CMDAT_BUS_WIDTH_BIT)
  #define MSC_CMDAT_BUS_WIDTH_1BIT	(0x0 << MSC_CMDAT_BUS_WIDTH_BIT) /* 1-bit data bus */
  #define MSC_CMDAT_BUS_WIDTH_4BIT	(0x2 << MSC_CMDAT_BUS_WIDTH_BIT) /* 4-bit data bus */
  #define MSC_CMDAT_BUS_WIDTH_8BIT	(0x3 << MSC_CMDAT_BUS_WIDTH_BIT) /* 8-bit data bus */
#define	MSC_CMDAT_DMA_EN		(1 << 8)
#define	MSC_CMDAT_INIT			(1 << 7)
#define	MSC_CMDAT_BUSY			(1 << 6)
#define	MSC_CMDAT_STREAM_BLOCK		(1 << 5)
#define	MSC_CMDAT_WRITE			(1 << 4)
#define	MSC_CMDAT_READ			(0 << 4)
#define	MSC_CMDAT_DATA_EN		(1 << 3)
#define	MSC_CMDAT_RESPONSE_BIT	0
#define	MSC_CMDAT_RESPONSE_MASK	(0x7 << MSC_CMDAT_RESPONSE_BIT)
  #define MSC_CMDAT_RESPONSE_NONE (0x0 << MSC_CMDAT_RESPONSE_BIT) /* No response */
  #define MSC_CMDAT_RESPONSE_R1	  (0x1 << MSC_CMDAT_RESPONSE_BIT) /* Format R1 and R1b */
  #define MSC_CMDAT_RESPONSE_R2	  (0x2 << MSC_CMDAT_RESPONSE_BIT) /* Format R2 */
  #define MSC_CMDAT_RESPONSE_R3	  (0x3 << MSC_CMDAT_RESPONSE_BIT) /* Format R3 */
  #define MSC_CMDAT_RESPONSE_R4	  (0x4 << MSC_CMDAT_RESPONSE_BIT) /* Format R4 */
  #define MSC_CMDAT_RESPONSE_R5	  (0x5 << MSC_CMDAT_RESPONSE_BIT) /* Format R5 */
  #define MSC_CMDAT_RESPONSE_R6	  (0x6 << MSC_CMDAT_RESPONSE_BIT) /* Format R6 */
  #define MSC_CMDAT_RESRONSE_R7   (0x7 << MSC_CMDAT_RESPONSE_BIT) /* Format R7 */

#define	CMDAT_DMA_EN	(1 << 8)
#define	CMDAT_INIT	(1 << 7)
#define	CMDAT_BUSY	(1 << 6)
#define	CMDAT_STREAM	(1 << 5)
#define	CMDAT_WRITE	(1 << 4)
#define	CMDAT_DATA_EN	(1 << 3)

/* MSC Interrupts Mask Register (MSC_IMASK) */
#define	MSC_IMASK_AUTO_CMD_DONE		(1 << 15)
#define	MSC_IMASK_DATA_FIFO_FULL	(1 << 14)
#define	MSC_IMASK_DATA_FIFO_EMP		(1 << 13)
#define	MSC_IMASK_CRC_RES_ERR		(1 << 12)
#define	MSC_IMASK_CRC_READ_ERR		(1 << 11)
#define	MSC_IMASK_CRC_WRITE_ERR		(1 << 10)
#define	MSC_IMASK_TIME_OUT_RES		(1 << 9)
#define	MSC_IMASK_TIME_OUT_READ		(1 << 8)
#define	MSC_IMASK_SDIO			(1 << 7)
#define	MSC_IMASK_TXFIFO_WR_REQ		(1 << 6)
#define	MSC_IMASK_RXFIFO_RD_REQ		(1 << 5)
#define	MSC_IMASK_END_CMD_RES		(1 << 2)
#define	MSC_IMASK_PRG_DONE		(1 << 1)
#define	MSC_IMASK_DATA_TRAN_DONE	(1 << 0)

/* MSC Interrupts Status Register (MSC_IREG) */
#define	MSC_IREG_AUTO_CMD_DONE		(1 << 15)
#define	MSC_IREG_DATA_FIFO_FULL		(1 << 14)
#define	MSC_IREG_DATA_FIFO_EMP		(1 << 13)
#define	MSC_IREG_CRC_RES_ERR		(1 << 12)
#define	MSC_IREG_CRC_READ_ERR		(1 << 11)
#define	MSC_IREG_CRC_WRITE_ERR		(1 << 10)
#define	MSC_IREG_TIMEOUT_RES		(1 << 9)
#define	MSC_IREG_TIMEOUT_READ		(1 << 8)
#define	MSC_IREG_SDIO			(1 << 7)
#define	MSC_IREG_TXFIFO_WR_REQ		(1 << 6)
#define	MSC_IREG_RXFIFO_RD_REQ		(1 << 5)
#define	MSC_IREG_END_CMD_RES		(1 << 2)
#define	MSC_IREG_PRG_DONE		(1 << 1)
#define	MSC_IREG_DATA_TRAN_DONE		(1 << 0)

/* MSC Low Power Mode Register (MSC_LPM) */
#define	MSC_SET_HISPD			(1 << 31)
#define	MSC_SET_LPM			(1 << 0)

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * MSC
 ***************************************************************************/
/* n = 0, 1 (MSC0, MSC1) */

#define __msc_start_op(n) \
	( REG_MSC_STRPCL(n) = MSC_STRPCL_START_OP | MSC_STRPCL_CLOCK_CONTROL_START )

#define __msc_set_resto(n, to)  	( REG_MSC_RESTO(n) = to )
#define __msc_set_rdto(n, to)   	( REG_MSC_RDTO(n) = to )
#define __msc_set_cmd(n, cmd)   	( REG_MSC_CMD(n) = cmd )
#define __msc_set_arg(n, arg)   	( REG_MSC_ARG(n) = arg )
#define __msc_set_nob(n, nob)      	( REG_MSC_NOB(n) = nob )
#define __msc_get_nob(n)        	( REG_MSC_NOB(n) )
#define __msc_set_blklen(n, len)        ( REG_MSC_BLKLEN(n) = len )
#define __msc_set_cmdat(n, cmdat)   	( REG_MSC_CMDAT(n) = cmdat )

#define __msc_set_cmdat_bus_width1(n) 			\
do { 							\
	REG_MSC_CMDAT(n) &= ~MSC_CMDAT_BUS_WIDTH_MASK; 	\
	REG_MSC_CMDAT(n) |= MSC_CMDAT_BUS_WIDTH_1BIT; 	\
} while(0)

#define __msc_set_cmdat_bus_width4(n) 			\
do { 							\
	REG_MSC_CMDAT(n) &= ~MSC_CMDAT_BUS_WIDTH_MASK; 	\
	REG_MSC_CMDAT(n) |= MSC_CMDAT_BUS_WIDTH_4BIT; 	\
} while(0)

#define __msc_set_cmdat_dma_en(n)       ( REG_MSC_CMDAT(n) |= MSC_CMDAT_DMA_EN )
#define __msc_set_cmdat_init(n) 	( REG_MSC_CMDAT(n) |= MSC_CMDAT_INIT )
#define __msc_set_cmdat_busy(n) 	( REG_MSC_CMDAT(n) |= MSC_CMDAT_BUSY )
#define __msc_set_cmdat_stream(n)       ( REG_MSC_CMDAT(n) |= MSC_CMDAT_STREAM_BLOCK )
#define __msc_set_cmdat_block(n)        ( REG_MSC_CMDAT(n) &= ~MSC_CMDAT_STREAM_BLOCK )
#define __msc_set_cmdat_read(n) 	( REG_MSC_CMDAT(n) &= ~MSC_CMDAT_WRITE )
#define __msc_set_cmdat_write(n)        ( REG_MSC_CMDAT(n) |= MSC_CMDAT_WRITE )
#define __msc_set_cmdat_data_en(n)      ( REG_MSC_CMDAT(n) |= MSC_CMDAT_DATA_EN )

/* r is MSC_CMDAT_RESPONSE_FORMAT_Rx or MSC_CMDAT_RESPONSE_FORMAT_NONE */
#define __msc_set_cmdat_res_format(n, r)				\
do { 								\
	REG_MSC_CMDAT(n) &= ~MSC_CMDAT_RESPONSE_MASK; 	\
	REG_MSC_CMDAT(n) |= (r); 					\
} while(0)

#define __msc_clear_cmdat(n) \
  REG_MSC_CMDAT(n) &= ~( MSC_CMDAT_STOP_ABORT | MSC_CMDAT_DMA_EN | MSC_CMDAT_INIT| \
  MSC_CMDAT_BUSY | MSC_CMDAT_STREAM_BLOCK | MSC_CMDAT_WRITE | \
  MSC_CMDAT_DATA_EN | MSC_CMDAT_RESPONSE_MASK )

#define __msc_get_imask(n) 		( REG_MSC_IMASK(n) )
#define __msc_mask_all_intrs(n) 	( REG_MSC_IMASK(n) = 0xff )
#define __msc_unmask_all_intrs(n) 	( REG_MSC_IMASK(n) = 0x00 )
#define __msc_mask_rd(n) 		( REG_MSC_IMASK(n) |= MSC_IMASK_RXFIFO_RD_REQ )
#define __msc_unmask_rd(n) 		( REG_MSC_IMASK(n) &= ~MSC_IMASK_RXFIFO_RD_REQ )
#define __msc_mask_wr(n) 		( REG_MSC_IMASK(n) |= MSC_IMASK_TXFIFO_WR_REQ )
#define __msc_unmask_wr(n) 		( REG_MSC_IMASK(n) &= ~MSC_IMASK_TXFIFO_WR_REQ )
#define __msc_mask_endcmdres(n) 	( REG_MSC_IMASK(n) |= MSC_IMASK_END_CMD_RES )
#define __msc_unmask_endcmdres(n) 	( REG_MSC_IMASK(n) &= ~MSC_IMASK_END_CMD_RES )
#define __msc_mask_datatrandone(n) 	( REG_MSC_IMASK(n) |= MSC_IMASK_DATA_TRAN_DONE )
#define __msc_unmask_datatrandone(n) 	( REG_MSC_IMASK(n) &= ~MSC_IMASK_DATA_TRAN_DONE )
#define __msc_mask_prgdone(n) 		( REG_MSC_IMASK(n) |= MSC_IMASK_PRG_DONE )
#define __msc_unmask_prgdone(n) 	( REG_MSC_IMASK(n) &= ~MSC_IMASK_PRG_DONE )

/* m=0,1,2,3,4,5,6,7 */
#define __msc_set_clkrt(n, m) 	\
do { 				\
	REG_MSC_CLKRT(n) = m;	\
} while(0)

#define __msc_get_ireg(n) 	        	( REG_MSC_IREG(n) )
#define __msc_ireg_rd(n) 	        	( REG_MSC_IREG(n) & MSC_IREG_RXFIFO_RD_REQ )
#define __msc_ireg_wr(n) 	        	( REG_MSC_IREG(n) & MSC_IREG_TXFIFO_WR_REQ )
#define __msc_ireg_end_cmd_res(n)       	( REG_MSC_IREG(n) & MSC_IREG_END_CMD_RES )
#define __msc_ireg_data_tran_done(n)     	( REG_MSC_IREG(n) & MSC_IREG_DATA_TRAN_DONE )
#define __msc_ireg_prg_done(n) 	        	( REG_MSC_IREG(n) & MSC_IREG_PRG_DONE )
#define __msc_ireg_clear_end_cmd_res(n)         ( REG_MSC_IREG(n) = MSC_IREG_END_CMD_RES )
#define __msc_ireg_clear_data_tran_done(n)      ( REG_MSC_IREG(n) = MSC_IREG_DATA_TRAN_DONE )
#define __msc_ireg_clear_prg_done(n)     	( REG_MSC_IREG(n) = MSC_IREG_PRG_DONE )

#define __msc_get_stat(n) 		( REG_MSC_STAT(n) )
#define __msc_stat_not_end_cmd_res(n) 	( (REG_MSC_STAT(n) & MSC_STAT_END_CMD_RES) == 0)
#define __msc_stat_crc_err(n) \
  ( REG_MSC_STAT(n) & (MSC_STAT_CRC_RES_ERR | MSC_STAT_CRC_READ_ERROR | MSC_STAT_CRC_WRITE_ERROR_YES) )
#define __msc_stat_res_crc_err(n) 	( REG_MSC_STAT(n) & MSC_STAT_CRC_RES_ERR )
#define __msc_stat_rd_crc_err(n) 	( REG_MSC_STAT(n) & MSC_STAT_CRC_READ_ERROR )
#define __msc_stat_wr_crc_err(n) 	( REG_MSC_STAT(n) & MSC_STAT_CRC_WRITE_ERROR_YES )
#define __msc_stat_resto_err(n) 	( REG_MSC_STAT(n) & MSC_STAT_TIME_OUT_RES )
#define __msc_stat_rdto_err(n) 		( REG_MSC_STAT(n) & MSC_STAT_TIME_OUT_READ )

#define __msc_rd_resfifo(n) 		( REG_MSC_RES(n) )
#define __msc_rd_rxfifo(n)  		( REG_MSC_RXFIFO(n) )
#define __msc_wr_txfifo(n, v)  		( REG_MSC_TXFIFO(n) = v )

#define __msc_reset(n) 						\
do { 								\
	REG_MSC_STRPCL(n) = MSC_STRPCL_RESET;			\
 	while (REG_MSC_STAT(n) & MSC_STAT_IS_RESETTING);		\
} while (0)

#define __msc_start_clk(n) 					\
do { 								\
	REG_MSC_STRPCL(n) = MSC_STRPCL_CLOCK_CONTROL_START;	\
} while (0)

#define __msc_stop_clk(n) 					\
do { 								\
	REG_MSC_STRPCL(n) = MSC_STRPCL_CLOCK_CONTROL_STOP;	\
} while (0)

#define MMC_CLK 19169200
#define SD_CLK  24576000

/* msc_clk should little than pclk and little than clk retrieve from card */
#define __msc_calc_clk_divisor(type,dev_clk,msc_clk,lv)		\
do {								\
	unsigned int rate, pclk, i;				\
	pclk = dev_clk;						\
	rate = type?SD_CLK:MMC_CLK;				\
  	if (msc_clk && msc_clk < pclk)				\
    		pclk = msc_clk;					\
	i = 0;							\
  	while (pclk < rate)					\
    	{							\
      		i ++;						\
      		rate >>= 1;					\
    	}							\
  	lv = i;							\
} while(0)

/* divide rate to little than or equal to 400kHz */
#define __msc_calc_slow_clk_divisor(type, lv)			\
do {								\
	unsigned int rate, i;					\
	rate = (type?SD_CLK:MMC_CLK)/1000/400;			\
	i = 0;							\
	while (rate > 0)					\
    	{							\
      		rate >>= 1;					\
      		i ++;						\
    	}							\
  	lv = i;							\
} while(0)

#endif /* __MIPS_ASSEMBLER */

#define NEMC_BASE	0xB3410000

/*************************************************************************
 * NEMC (External Memory Controller for NAND)
 *************************************************************************/

#define NEMC_NFCSR	(NEMC_BASE + 0x50) /* NAND Flash Control/Status Register */
#define NEMC_SMCR	(NEMC_BASE + 0x14)  /* Static Memory Control Register 1 */
#define NEMC_PNCR 	(NEMC_BASE + 0x100)
#define NEMC_PNDR 	(NEMC_BASE + 0x104)
#define NEMC_BITCNT	(NEMC_BASE + 0x108)

#define REG_NEMC_NFCSR	REG32(NEMC_NFCSR)
#define REG_NEMC_SMCR1	REG32(NEMC_SMCR)
#define REG_NEMC_PNCR 	REG32(NEMC_PNCR)
#define REG_NEMC_PNDR 	REG32(NEMC_PNDR)
#define REG_NEMC_BITCNT	REG32(NEMC_BITCNT)

/* NAND Flash Control/Status Register */
#define NEMC_NFCSR_NFCE4	(1 << 7) /* NAND Flash Enable */
#define NEMC_NFCSR_NFE4		(1 << 6) /* NAND Flash FCE# Assertion Enable */
#define NEMC_NFCSR_NFCE3	(1 << 5)
#define NEMC_NFCSR_NFE3		(1 << 4)
#define NEMC_NFCSR_NFCE2	(1 << 3)
#define NEMC_NFCSR_NFE2		(1 << 2)
#define NEMC_NFCSR_NFCE1	(1 << 1)
#define NEMC_NFCSR_NFE1		(1 << 0)

#define UDC_BASE	0xB3440000

/*************************************************************************
 * USB Device
 *************************************************************************/
#define USB_BASE  UDC_BASE

#define USB_FADDR		(USB_BASE + 0x00) /* Function Address 8-bit */
#define USB_POWER		(USB_BASE + 0x01) /* Power Managemetn 8-bit */
#define USB_INTRIN		(USB_BASE + 0x02) /* Interrupt IN 16-bit */
#define USB_INTROUT		(USB_BASE + 0x04) /* Interrupt OUT 16-bit */
#define USB_INTRINE		(USB_BASE + 0x06) /* Intr IN enable 16-bit */
#define USB_INTROUTE		(USB_BASE + 0x08) /* Intr OUT enable 16-bit */
#define USB_INTRUSB		(USB_BASE + 0x0a) /* Interrupt USB 8-bit */
#define USB_INTRUSBE		(USB_BASE + 0x0b) /* Interrupt USB Enable 8-bit */
#define USB_FRAME		(USB_BASE + 0x0c) /* Frame number 16-bit */
#define USB_INDEX		(USB_BASE + 0x0e) /* Index register 8-bit */
#define USB_TESTMODE		(USB_BASE + 0x0f) /* USB test mode 8-bit */

#define USB_CSR0		(USB_BASE + 0x12) /* EP0 CSR 16-bit */
#define USB_COUNT0		(USB_BASE + 0x18) /* EP0 OUT FIFO count 8-bit */

#define USB_INMAXP		(USB_BASE + 0x10) /* EP1-15 IN Max Pkt Size 16-bit */
#define USB_INCSR		(USB_BASE + 0x12) /* EP1-15 IN CSR LSB 8/16bit */
#define USB_INCSRH		(USB_BASE + 0x13) /* EP1-15 IN CSR MSB 8-bit */
#define USB_OUTMAXP		(USB_BASE + 0x14) /* EP1-15 OUT Max Pkt Size 16-bit */
#define USB_OUTCSR		(USB_BASE + 0x16) /* EP1-15 OUT CSR LSB 8/16bit */
#define USB_OUTCSRH		(USB_BASE + 0x17) /* EP1-15 OUT CSR MSB 8-bit */
#define USB_OUTCOUNT		(USB_BASE + 0x18) /* EP1-15 OUT FIFO count 16-bit */

#define USB_CONFIGDATA          (USB_BASE + 0x1f) /* Fixed config */

#define USB_FIFO_EP(n)		(USB_BASE + (n)*4 + 0x20)

#define USB_HWVERS              (USB_BASE + 0x6c)

#define USB_EPINFO		(USB_BASE + 0x78) /* Endpoint information */
#define USB_RAMINFO		(USB_BASE + 0x79) /* RAM information */

#define USB_INTR		(USB_BASE + 0x200) /* DMA pending interrupts */
#define USB_CNTL(n)		(USB_BASE + (n)*0x10 + 0x204) /* DMA channel n control */
#define USB_ADDR(n)		(USB_BASE + (n)*0x10 + 0x208) /* DMA channel n AHB memory addr */
#define USB_COUNT(n)		(USB_BASE + (n)*0x10 + 0x20c) /* DMA channel n byte count */

/* Power register bit masks */
#define USB_POWER_SUSPENDM	0x01
#define USB_POWER_RESUME	0x04
#define USB_POWER_HSMODE	0x10
#define USB_POWER_HSENAB	0x20
#define USB_POWER_SOFTCONN	0x40

/* Interrupt register bit masks */
#define USB_INTR_SUSPEND	0x01
#define USB_INTR_RESUME		0x02
#define USB_INTR_RESET		0x04

#define USB_INTR_EP(n)		(1 << (n))

/* CSR0 bit masks */
#define USB_CSR0_OUTPKTRDY	0x01
#define USB_CSR0_INPKTRDY	0x02
#define USB_CSR0_SENTSTALL	0x04
#define USB_CSR0_DATAEND	0x08
#define USB_CSR0_SETUPEND	0x10
#define USB_CSR0_SENDSTALL	0x20
#define USB_CSR0_SVDOUTPKTRDY	0x40
#define USB_CSR0_SVDSETUPEND	0x80
#define USB_CSR0_FLUSHFIFO	0x100

/* Endpoint CSR register bits */
#define USB_INCSRH_AUTOSET	0x80
#define USB_INCSRH_ISO		0x40
#define USB_INCSRH_MODE		0x20
#define USB_INCSRH_DMAREQENAB	0x10
#define USB_INCSRH_FRCDATATOG	0x08
#define USB_INCSRH_DMAREQMODE	0x04
#define USB_INCSR_INCOMPTX      0x80
#define USB_INCSR_CDT		0x40
#define USB_INCSR_SENTSTALL	0x20
#define USB_INCSR_SENDSTALL	0x10
#define USB_INCSR_FF		0x08
#define USB_INCSR_UNDERRUN	0x04
#define USB_INCSR_FFNOTEMPT	0x02
#define USB_INCSR_INPKTRDY	0x01
#define USB_OUTCSRH_AUTOCLR	0x80
#define USB_OUTCSRH_ISO		0x40
#define USB_OUTCSRH_DMAREQENAB	0x20
#define USB_OUTCSRH_DNYT	0x10
#define USB_OUTCSRH_DMAREQMODE	0x08
#define USB_OUTCSR_CDT		0x80
#define USB_OUTCSR_SENTSTALL	0x40
#define USB_OUTCSR_SENDSTALL	0x20
#define USB_OUTCSR_FF		0x10
#define USB_OUTCSR_DATAERR	0x08
#define USB_OUTCSR_OVERRUN	0x04
#define USB_OUTCSR_FFFULL	0x02
#define USB_OUTCSR_OUTPKTRDY	0x01

/* Testmode register bits */
#define USB_TEST_SE0NAK		0x01
#define USB_TEST_J		0x02
#define USB_TEST_K		0x04
#define USB_TEST_PACKET		0x08
#define USB_TEST_FORCE_HS	0x10
#define USB_TEST_FORCE_FS	0x20
#define USB_TEST_ALL		( USB_TEST_SE0NAK | USB_TEST_J         \
				| USB_TEST_K | USB_TEST_PACKET         \
				| USB_TEST_FORCE_HS | USB_TEST_FORCE_FS)

/* DMA control bits */
#define USB_CNTL_ENA		0x01
#define USB_CNTL_DIR_IN		0x02
#define USB_CNTL_MODE_1		0x04
#define USB_CNTL_INTR_EN	0x08
#define USB_CNTL_EP(n)		((n) << 4)
#define USB_CNTL_BURST_0	(0 << 9)
#define USB_CNTL_BURST_4	(1 << 9)
#define USB_CNTL_BURST_8	(2 << 9)
#define USB_CNTL_BURST_16	(3 << 9)

/* USB HW revision */
#define USB_HWVERS_MAJOR(x)    ((x >> 10) & 0x1f)
#define USB_HWVERS_MINOR(x)    (x & 0x3ff)

/* DMA interrupt bits */
#define USB_INTR_DMA_BULKIN	1
#define USB_INTR_DMA_BULKOUT	2

#define REG_USB_FADDR		REG8(USB_FADDR)
#define REG_USB_POWER		REG8(USB_POWER)
#define REG_USB_INTRIN		REG16(USB_INTRIN)
#define REG_USB_INTROUT		REG16(USB_INTROUT)
#define REG_USB_INTRINE		REG16(USB_INTRINE)
#define REG_USB_INTROUTE	REG16(USB_INTROUTE)
#define REG_USB_INTRUSB		REG8(USB_INTRUSB)
#define REG_USB_INTRUSBE	REG8(USB_INTRUSBE)
#define REG_USB_FRAME		REG16(USB_FRAME)
#define REG_USB_INDEX		REG8(USB_INDEX)
#define REG_USB_TESTMODE	REG8(USB_TESTMODE)

#define REG_USB_CSR0		REG16(USB_CSR0)
#define REG_USB_COUNT0		REG8(USB_COUNT0)

#define REG_USB_INMAXP		REG16(USB_INMAXP)
#define REG_USB_INCSR		REG16(USB_INCSR)
#define REG_USB_INCSRH		REG8(USB_INCSRH)
#define REG_USB_OUTMAXP		REG16(USB_OUTMAXP)
#define REG_USB_OUTCSR		REG16(USB_OUTCSR)
#define REG_USB_OUTCSRH		REG8(USB_OUTCSRH)
#define REG_USB_OUTCOUNT	REG16(USB_OUTCOUNT)

#define REG_USB_CONFIGDATA      REG8(USB_CONFIGDATA)
#define REG_USB_FIFO_EP(n)	REG32(USB_FIFO_EP(n))

#define REG_USB_INTR		REG8(USB_INTR)
#define REG_USB_CNTL(n)		REG16(USB_CNTL(n))
#define REG_USB_ADDR(n)		REG32(USB_ADDR(n))
#define REG_USB_COUNT(n)	REG32(USB_COUNT(n))

#define REG_USB_HWVERS          REG16(USB_HWVERS)

#define REG_USB_EPINFO		REG8(USB_EPINFO)
#define REG_USB_RAMINFO		REG8(USB_RAMINFO)

/*
 * One wire bus interface(OWI) address definition
 */
#define	OWI_BASE	0xb0072000

/*
 * OWI registers offset address definition
 */
#define OWI_OWICFG_OFFSET	(0x00)	/* rw, 8, 0x00 */
#define OWI_OWICTL_OFFSET	(0x04)	/* rw, 8, 0x00 */
#define OWI_OWISTS_OFFSET	(0x08)	/* rw, 8, 0x00 */
#define OWI_OWIDAT_OFFSET	(0x0c)	/* rw, 8, 0x00 */
#define OWI_OWIDIV_OFFSET	(0x10)	/* rw, 8, 0x00 */

/*
 * OWI registers address definition
 */
#define OWI_OWICFG	(OWI_BASE + OWI_OWICFG_OFFSET)
#define OWI_OWICTL	(OWI_BASE + OWI_OWICTL_OFFSET)
#define OWI_OWISTS	(OWI_BASE + OWI_OWISTS_OFFSET)
#define OWI_OWIDAT	(OWI_BASE + OWI_OWIDAT_OFFSET)
#define OWI_OWIDIV	(OWI_BASE + OWI_OWIDIV_OFFSET)

/*
 * OWI registers common define
 */

/* OWI configure register(OWICFG) */
#define OWICFG_MODE	BIT7
#define OWICFG_RDDATA	BIT6
#define OWICFG_WRDATA	BIT5
#define OWICFG_RDST	BIT4
#define OWICFG_WR1RD	BIT3
#define OWICFG_WR0	BIT2
#define OWICFG_RST	BIT1
#define OWICFG_ENA	BIT0

/* OWI control register(OWICTL) */
#define OWICTL_EBYTE	BIT2
#define OWICTL_EBIT	BIT1
#define OWICTL_ERST	BIT0

/* OWI status register(OWISTS) */
#define OWISTS_PST		BIT7
#define OWISTS_BYTE_RDY		BIT2
#define OWISTS_BIT_RDY		BIT1
#define OWISTS_PST_RDY		BIT0

/* OWI clock divide register(OWIDIV) */
#define OWIDIV_CLKDIV_LSB	0
#define OWIDIV_CLKDIV_MASK	BITS_H2L(5, OWIDIV_CLKDIV_LSB)

#ifndef __MIPS_ASSEMBLER

/* Basic ops */
#define REG_OWI_OWICFG  REG8(OWI_OWICFG)
#define REG_OWI_OWICTL  REG8(OWI_OWICTL)
#define REG_OWI_OWISTS  REG8(OWI_OWISTS)
#define REG_OWI_OWIDAT  REG8(OWI_OWIDAT)
#define REG_OWI_OWIDIV  REG8(OWI_OWIDIV)

#endif /* __MIPS_ASSEMBLER */

/*
 * Pulse-code modulation module(PCM) address definition
 */
#define	PCM_BASE        0xb0071000

/*
 * pcm number, jz4760x has only PCM0
 */

#define PCM0		0
#define PCM1		1

/* PCM groups offset */
#define PCM_GOS		0x3000

/*
 * PCM registers offset address definition
 */
#define PCM_PCTL_OFFSET		(0x00)	/* rw, 32, 0x00000000 */
#define PCM_PCFG_OFFSET		(0x04)  /* rw, 32, 0x00000110 */
#define PCM_PDP_OFFSET		(0x08)  /* rw, 32, 0x00000000 */
#define PCM_PINTC_OFFSET	(0x0c)  /* rw, 32, 0x00000000 */
#define PCM_PINTS_OFFSET	(0x10)  /* rw, 32, 0x00000100 */
#define PCM_PDIV_OFFSET		(0x14)  /* rw, 32, 0x00000001 */

/*
 * PCM registers address definition
 */
#define PCM_PCTL(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PCTL_OFFSET)
#define PCM_PCFG(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PCFG_OFFSET)
#define PCM_PDP(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PDP_OFFSET)
#define PCM_PINTC(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PINTC_OFFSET)
#define PCM_PINTS(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PINTS_OFFSET)
#define PCM_PDIV(n)	(PCM_BASE + (n) * PCM_GOS + PCM_PDIV_OFFSET)

/*
 * CPM registers common define
 */

/* PCM controller control register (PCTL) */
#define PCTL_ERDMA	BIT9
#define PCTL_ETDMA	BIT8
#define PCTL_LSMP	BIT7
#define PCTL_ERPL	BIT6
#define PCTL_EREC	BIT5
#define PCTL_FLUSH	BIT4
#define PCTL_RST	BIT3
#define PCTL_CLKEN	BIT1
#define PCTL_PCMEN	BIT0

/* PCM controller configure register (PCFG) */
#define PCFG_ISS_16BIT		BIT12
#define PCFG_OSS_16BIT		BIT11
#define PCFG_IMSBPOS		BIT10
#define PCFG_OMSBPOS		BIT9
#define PCFG_MODE_SLAVE		BIT0

#define PCFG_SLOT_LSB		13
#define PCFG_SLOT_MASK		BITS_H2L(14, PCFG_SLOT_LSB)
#define PCFG_SLOT(val)		((val) << PCFG_SLOT_LSB)

#define	PCFG_RFTH_LSB		5
#define	PCFG_RFTH_MASK		BITS_H2L(8, PCFG_RFTH_LSB)

#define	PCFG_TFTH_LSB		1
#define	PCFG_TFTH_MASK		BITS_H2L(4, PCFG_TFTH_LSB)

/* PCM controller interrupt control register(PINTC) */
#define PINTC_ETFS	BIT3
#define PINTC_ETUR	BIT2
#define PINTC_ERFS	BIT1
#define PINTC_EROR	BIT0

/* PCM controller interrupt status register(PINTS) */
#define PINTS_RSTS	BIT14
#define PINTS_TFS	BIT8
#define PINTS_TUR	BIT7
#define PINTS_RFS	BIT1
#define PINTS_ROR	BIT0

#define PINTS_TFL_LSB		9
#define PINTS_TFL_MASK		BITS_H2L(13, PINTS_TFL_LSB)

#define PINTS_RFL_LSB		2
#define PINTS_RFL_MASK		BITS_H2L(6, PINTS_RFL_LSB)

/* PCM controller clock division register(PDIV) */
#define PDIV_SYNL_LSB		11
#define PDIV_SYNL_MASK		BITS_H2L(16, PDIV_SYNL_LSB)

#define PDIV_SYNDIV_LSB		6
#define PDIV_SYNDIV_MASK	BITS_H2L(10, PDIV_SYNDIV_LSB)

#define PDIV_CLKDIV_LSB		0
#define PDIV_CLKDIV_MASK	BITS_H2L(5, PDIV_CLKDIV_LSB)

#ifndef __MIPS_ASSEMBLER

#define REG_PCM_PCTL(n)		REG32(PCM_PCTL(n))
#define REG_PCM_PCFG(n)		REG32(PCM_PCFG(n))
#define REG_PCM_PDP(n)		REG32(PCM_PDP(n))
#define REG_PCM_PINTC(n)	REG32(PCM_PINTC(n))
#define REG_PCM_PINTS(n)	REG32(PCM_PINTS(n))
#define REG_PCM_PDIV(n)		REG32(PCM_PDIV(n))

/*
 * PCM_DIN, PCM_DOUT, PCM_CLK, PCM_SYN
 */
#define __gpio_as_pcm(n) 						\
do {									\
	switch(n) {							\
	case PCM0:		__gpio_as_pcm0();break;			\
	case PCM1:		__gpio_as_pcm1();break;			\
	}								\
									\
} while (0)

#define __pcm_enable(n)			(REG_PCM_PCTL(n) |= PCTL_PCMEN)
#define __pcm_disable(n)		(REG_PCM_PCTL(n) &= ~PCTL_PCMEN)

#define __pcm_clk_enable(n)		(REG_PCM_PCTL(n) |= PCTL_CLKEN)
#define __pcm_clk_disable(n)		(REG_PCM_PCTL(n) &= ~PCTL_CLKEN)

#define __pcm_reset(n)			(REG_PCM_PCTL(n) |= PCTL_RST)
#define __pcm_flush_fifo(n)		(REG_PCM_PCTL(n) |= PCTL_FLUSH)

#define __pcm_enable_record(n)		(REG_PCM_PCTL(n) |= PCTL_EREC)
#define __pcm_disable_record(n)		(REG_PCM_PCTL(n) &= ~PCTL_EREC)
#define __pcm_enable_playback(n)	(REG_PCM_PCTL(n) |= PCTL_ERPL)
#define __pcm_disable_playback(n)	(REG_PCM_PCTL(n) &= ~PCTL_ERPL)

#define __pcm_enable_rxfifo(n)		__pcm_enable_record(n)
#define __pcm_disable_rxfifo(n)		__pcm_disable_record(n)
#define __pcm_enable_txfifo(n)		__pcm_enable_playback(n)
#define __pcm_disable_txfifo(n)		__pcm_disable_playback(n)

#define __pcm_last_sample(n)		(REG_PCM_PCTL(n) |= PCTL_LSMP)
#define __pcm_zero_sample(n)		(REG_PCM_PCTL(n) &= ~PCTL_LSMP)

#define __pcm_enable_transmit_dma(n)	(REG_PCM_PCTL(n) |= PCTL_ETDMA)
#define __pcm_disable_transmit_dma(n)	(REG_PCM_PCTL(n) &= ~PCTL_ETDMA)
#define __pcm_enable_receive_dma(n)	(REG_PCM_PCTL(n) |= PCTL_ERDMA)
#define __pcm_disable_receive_dma(n)	(REG_PCM_PCTL(n) &= ~PCTL_ERDMA)

#define __pcm_as_master(n)		(REG_PCM_PCFG(n) &= ~PCFG_MODE_SLAVE)
#define __pcm_as_slave(n)		(REG_PCM_PCFG(n) |= PCFG_MODE_SLAVE)

#define __pcm_set_transmit_trigger(n, val)		\
do {							\
	REG_PCM_PCFG(n) &= ~PCFG_TFTH_MASK;		\
	REG_PCM_PCFG(n) |= ((val) << PCFG_TFTH_LSB);	\
							\
} while(0)

#define __pcm_set_receive_trigger(n, val)		\
do {							\
	REG_PCM_PCFG(n) &= ~PCFG_RFTH_MASK;		\
	REG_PCM_PCFG(n) |= ((val) << PCFG_RFTH_LSB);	\
							\
} while(0)

#define __pcm_omsb_same_sync(n)   	(REG_PCM_PCFG(n) &= ~PCFG_OMSBPOS)
#define __pcm_omsb_next_sync(n)   	(REG_PCM_PCFG(n) |= PCFG_OMSBPOS)

#define __pcm_imsb_same_sync(n)   	(REG_PCM_PCFG(n) &= ~PCFG_IMSBPOS)
#define __pcm_imsb_next_sync(n)   	(REG_PCM_PCFG(n) |= PCFG_IMSBPOS)

#define __pcm_set_iss(n, val)				\
do {							\
	if ((val) == 16)				\
		REG_PCM_PCFG(n) |= PCFG_ISS_16BIT;	\
	else						\
		REG_PCM_PCFG(n) &= ~PCFG_ISS_16BIT;	\
							\
} while (0)

#define __pcm_set_oss(n, val)				\
do {							\
	if ((val) == 16)				\
		REG_PCM_PCFG(n) |= PCFG_OSS_16BIT;	\
	else						\
		REG_PCM_PCFG(n) &= ~PCFG_OSS_16BIT;	\
							\
} while (0)						\

#define __pcm_set_valid_slot(n, val)					\
	(REG_PCM_PCFG(n) = (REG_PCM_PCFG(n) & ~PCFG_SLOT_MASK) | PCFG_SLOT(val))

#define __pcm_write_data(n, val)	(REG_PCM_PDP(n) = (val))
#define __pcm_read_data(n)		(REG_PCM_PDP(n))

#define __pcm_enable_tfs_intr(n)	(REG_PCM_PINTC(n) |= PINTC_ETFS)
#define __pcm_disable_tfs_intr(n)	(REG_PCM_PINTC(n) &= ~PINTC_ETFS)

#define __pcm_enable_tur_intr(n)	(REG_PCM_PINTC(n) |= PINTC_ETUR)
#define __pcm_disable_tur_intr(n)	(REG_PCM_PINTC(n) &= ~PINTC_ETUR)

#define __pcm_enable_rfs_intr(n)	(REG_PCM_PINTC(n) |= PINTC_ERFS)
#define __pcm_disable_rfs_intr(n)	(REG_PCM_PINTC(n) &= ~PINTC_ERFS)

#define __pcm_enable_ror_intr(n)	(REG_PCM_PINTC(n) |= PINTC_EROR)
#define __pcm_disable_ror_intr(n)	(REG_PCM_PINTC(n) &= ~PINTC_EROR)

#define __pcm_ints_valid_tx(n)		(((REG_PCM_PINTS(n) & PINTS_TFL_MASK) >> PINTS_TFL_LSB))
#define __pcm_ints_valid_rx(n)		(((REG_PCM_PINTS(n) & PINTS_RFL_MASK) >> PINTS_RFL_LSB))

#define __pcm_set_clk_div(n, val)					\
	(REG_PCM_PDIV(n) = (REG_PCM_PDIV(n) & ~PDIV_CLKDIV_MASK) | ((val) << PDIV_CLKDIV_LSB))

#define __pcm_set_clk_rate(n, sysclk, pcmclk)				\
	__pcm_set_clk_div((n), ((sysclk) / (pcmclk) - 1))

#define __pcm_set_sync_div(n, val)					\
	(REG_PCM_PDIV(n) = (REG_PCM_PDIV(n) & ~PDIV_SYNDIV_MASK) | ((val) << PDIV_SYNDIV_LSB))

#define __pcm_set_sync_rate(n, pcmclk, sync)				\
	__pcm_set_sync_div((n), ((pcmclk) / (8 * (sync)) - 1))

#define __pcm_set_sync_len(n, val)					\
	(REG_PCM_PDIV(n) = (REG_PCM_PDIV(n) & ~PDIV_SYNL_MASK) | ((val) << PDIV_SYNL_LSB))

#endif /* __MIPS_ASSEMBLER */

/*
 * Real time clock module(RTC) address definition
 */
#define	RTC_BASE	0xb0003000

/*
 * RTC registers offset address definition
 */
#define RTC_RTCCR_OFFSET	(0x00)	/* rw, 32, 0x00000081 */
#define RTC_RTCSR_OFFSET	(0x04)	/* rw, 32, 0x???????? */
#define RTC_RTCSAR_OFFSET	(0x08)	/* rw, 32, 0x???????? */
#define RTC_RTCGR_OFFSET	(0x0c)	/* rw, 32, 0x0??????? */

#define RTC_HCR_OFFSET		(0x20)  /* rw, 32, 0x00000000 */
#define RTC_HWFCR_OFFSET	(0x24)  /* rw, 32, 0x0000???0 */
#define RTC_HRCR_OFFSET		(0x28)  /* rw, 32, 0x00000??0 */
#define RTC_HWCR_OFFSET		(0x2c)  /* rw, 32, 0x00000008 */
#define RTC_HWRSR_OFFSET	(0x30)  /* rw, 32, 0x00000000 */
#define RTC_HSPR_OFFSET		(0x34)  /* rw, 32, 0x???????? */
#define RTC_WENR_OFFSET		(0x3c)  /* rw, 32, 0x00000000 */

/*
 * RTC registers address definition
 */
#define RTC_RTCCR	(RTC_BASE + RTC_RTCCR_OFFSET)
#define RTC_RTCSR	(RTC_BASE + RTC_RTCSR_OFFSET)
#define RTC_RTCSAR	(RTC_BASE + RTC_RTCSAR_OFFSET)
#define RTC_RTCGR	(RTC_BASE + RTC_RTCGR_OFFSET)

#define RTC_HCR		(RTC_BASE + RTC_HCR_OFFSET)
#define RTC_HWFCR	(RTC_BASE + RTC_HWFCR_OFFSET)
#define RTC_HRCR	(RTC_BASE + RTC_HRCR_OFFSET)
#define RTC_HWCR	(RTC_BASE + RTC_HWCR_OFFSET)
#define RTC_HWRSR	(RTC_BASE + RTC_HWRSR_OFFSET)
#define RTC_HSPR	(RTC_BASE + RTC_HSPR_OFFSET)
#define RTC_WENR	(RTC_BASE + RTC_WENR_OFFSET)

/*
 * RTC registers common define
 */

/* RTC control register(RTCCR) */
#define RTCCR_WRDY		BIT7
#define RTCCR_1HZ		BIT6
#define RTCCR_1HZIE		BIT5
#define RTCCR_AF		BIT4
#define RTCCR_AIE		BIT3
#define RTCCR_AE		BIT2
#define RTCCR_SELEXC		BIT1
#define RTCCR_RTCE		BIT0

/* RTC regulator register(RTCGR) */
#define RTCGR_LOCK		BIT31

#define RTCGR_ADJC_LSB		16
#define RTCGR_ADJC_MASK		BITS_H2L(25, RTCGR_ADJC_LSB)

#define RTCGR_NC1HZ_LSB		0
#define RTCGR_NC1HZ_MASK	BITS_H2L(15, RTCGR_NC1HZ_LSB)

/* Hibernate control register(HCR) */
#define HCR_PD			BIT0

/* Hibernate wakeup filter counter register(HWFCR) */
#define HWFCR_LSB		5
#define HWFCR_MASK		BITS_H2L(15, HWFCR_LSB)
#define HWFCR_WAIT_TIME(ms)	(((ms) << HWFCR_LSB) > HWFCR_MASK ? HWFCR_MASK : ((ms) << HWFCR_LSB))

/* Hibernate reset counter register(HRCR) */
#define HRCR_LSB		5
#define HRCR_MASK		BITS_H2L(11, HRCR_LSB)
#define HRCR_WAIT_TIME(ms)     (((ms) << HRCR_LSB) > HRCR_MASK ? HRCR_MASK : ((ms) << HRCR_LSB))

/* Hibernate wakeup control register(HWCR) */
#define HWCR_EPDET		BIT3
#define HWCR_WKUPVL		BIT2
#define HWCR_EALM		BIT0

/* Hibernate wakeup status register(HWRSR) */
#define HWRSR_APD		BIT8
#define HWRSR_HR		BIT5
#define HWRSR_PPR		BIT4
#define HWRSR_PIN		BIT1
#define HWRSR_ALM		BIT0

/* write enable pattern register(WENR) */
#define WENR_WEN		BIT31

#define WENR_WENPAT_LSB		0
#define WENR_WENPAT_MASK	BITS_H2L(15, WENR_WENPAT_LSB)
#define WENR_WENPAT_WRITABLE	(0xa55a)

/* Hibernate scratch pattern register(HSPR) */
#define HSPR_RTCV               0x52544356      /* The value is 'RTCV', means rtc is valid */

#ifndef __MIPS_ASSEMBLER

/* Waiting for the RTC register writing finish */
#define __wait_write_ready()						\
do {									\
	int timeout = 0x1000;						\
	while (!(rtc_read_reg(RTC_RTCCR) & RTCCR_WRDY) && timeout--);	\
}while(0);

/* Waiting for the RTC register writable */
#define __wait_writable()						\
do {									\
	int timeout = 0x1000;						\
	__wait_write_ready();						\
	OUTREG32(RTC_WENR, WENR_WENPAT_WRITABLE);			\
	__wait_write_ready();						\
	while (!(rtc_read_reg(RTC_WENR) & WENR_WEN) && timeout--);	\
}while(0);

/* Basic RTC ops */
#define rtc_read_reg(reg)				\
({							\
	unsigned int data, timeout = 0x10000;		\
	do {						\
		data = INREG32(reg);			\
	} while (INREG32(reg) != data && timeout--);	\
	data;						\
})

#define rtc_write_reg(reg, data)			\
do {							\
	__wait_writable();				\
	OUTREG32(reg, data);				\
	__wait_write_ready();				\
}while(0);

#define rtc_set_reg(reg, data)	rtc_write_reg(reg, rtc_read_reg(reg) | (data))
#define rtc_clr_reg(reg, data)	rtc_write_reg(reg, rtc_read_reg(reg) & ~(data))

#endif /* __MIPS_ASSEMBLER */

/*
 * SAR A/D Controller(SADC) address definition
 */
#define	SADC_BASE		0xb0070000

/*
 * SADC registers offset definition
 */
#define SADC_ADENA_OFFSET	(0x00)	/* rw,  8, 0x00 */
#define SADC_ADCFG_OFFSET       (0x04)  /* rw, 32, 0x0002000c */
#define SADC_ADCTRL_OFFSET      (0x08)  /* rw,  8, 0x3f */
#define SADC_ADSTATE_OFFSET     (0x0c)  /* rw,  8, 0x00 */
#define SADC_ADSAME_OFFSET    	(0x10)  /* rw, 16, 0x0000 */
#define SADC_ADWAIT_OFFSET    	(0x14)  /* rw, 16, 0x0000 */
#define SADC_ADTCH_OFFSET       (0x18)  /* rw, 32, 0x00000000 */
#define SADC_ADVDAT_OFFSET      (0x1c)  /* rw, 16, 0x0000 */
#define SADC_ADADAT_OFFSET      (0x20)  /* rw, 16, 0x0000 */
#define SADC_ADFLT_OFFSET       (0x24)  /* rw, 16, 0x0000 */
#define SADC_ADCLK_OFFSET       (0x28)  /* rw, 32, 0x00000000 */

/*
 * SADC registers address definition
 */
#define SADC_ADENA		(SADC_BASE + SADC_ADENA_OFFSET)	 /* ADC Enable Register */
#define SADC_ADCFG		(SADC_BASE + SADC_ADCFG_OFFSET)	 /* ADC Configure Register */
#define SADC_ADCTRL		(SADC_BASE + SADC_ADCTRL_OFFSET) /* ADC Control Register */
#define SADC_ADSTATE		(SADC_BASE + SADC_ADSTATE_OFFSET)/* ADC Status Register*/
#define SADC_ADSAME		(SADC_BASE + SADC_ADSAME_OFFSET) /* ADC Same Point Time Register */
#define SADC_ADWAIT		(SADC_BASE + SADC_ADWAIT_OFFSET) /* ADC Wait Time Register */
#define SADC_ADTCH		(SADC_BASE + SADC_ADTCH_OFFSET)  /* ADC Touch Screen Data Register */
#define SADC_ADVDAT		(SADC_BASE + SADC_ADVDAT_OFFSET) /* ADC VBAT Data Register */
#define SADC_ADADAT		(SADC_BASE + SADC_ADADAT_OFFSET) /* ADC AUX Data Register */
#define SADC_ADFLT		(SADC_BASE + SADC_ADFLT_OFFSET)  /* ADC Filter Register */
#define SADC_ADCLK		(SADC_BASE + SADC_ADCLK_OFFSET)  /* ADC Clock Divide Register */

/*
 * SADC registers common define
 */

/* ADC Enable Register (ADENA) */
#define ADENA_POWER		BIT7
#define ADENA_SLP_MD		BIT6
#define ADENA_PENDEN		BIT3
#define ADENA_TCHEN		BIT2
#define ADENA_VBATEN		BIT1
#define ADENA_AUXEN		BIT0

/* ADC Configure Register (ADCFG) */
#define ADCFG_SPZZ           	BIT31
#define ADCFG_VBAT_SEL		BIT30
#define ADCFG_DMA_EN		BIT15

#define ADCFG_XYZ_LSB		13
#define ADCFG_XYZ_MASK		BITS_H2L(14, ADCFG_XYZ_LSB)
 #define ADCFG_XYZ_XYS		(0x0 << ADCFG_XYZ_LSB)
 #define ADCFG_XYZ_XYD		(0x1 << ADCFG_XYZ_LSB)
 #define ADCFG_XYZ_XYZ1Z2	(0x2 << ADCFG_XYZ_LSB)

#define ADCFG_SNUM_LSB		10
#define ADCFG_SNUM_MASK		BITS_H2L(12, ADCFG_SNUM_LSB)
 #define ADCFG_SNUM(n)          (((n) <= 6 ? ((n)-1) : ((n)-2)) << ADCFG_SNUM_LSB)

#define ADCFG_CMD_LSB		0
#define ADCFG_CMD_MASK		BITS_H2L(1, ADCFG_CMD_LSB)
 #define ADCFG_CMD_AUX(n)	((n) << ADCFG_CMD_LSB)

/* ADC Control Register (ADCCTRL) */
#define ADCTRL_SLPENDM		BIT5
#define ADCTRL_PENDM		BIT4
#define ADCTRL_PENUM		BIT3
#define ADCTRL_DTCHM		BIT2
#define ADCTRL_VRDYM		BIT1
#define ADCTRL_ARDYM		BIT0
#define ADCTRL_MASK_ALL         (ADCTRL_SLPENDM | ADCTRL_PENDM | ADCTRL_PENUM \
                                | ADCTRL_DTCHM | ADCTRL_VRDYM | ADCTRL_ARDYM)

/*  ADC Status Register  (ADSTATE) */
#define ADSTATE_SLP_RDY		BIT7
#define ADSTATE_SLPEND		BIT5
#define ADSTATE_PEND		BIT4
#define ADSTATE_PENU		BIT3
#define ADSTATE_DTCH		BIT2
#define ADSTATE_VRDY		BIT1
#define ADSTATE_ARDY		BIT0

/* ADC Same Point Time Register (ADSAME) */
#define ADSAME_SCNT_LSB		0
#define ADSAME_SCNT_MASK	BITS_H2L(15, ADSAME_SCNT_LSB)

/* ADC Wait Pen Down Time Register (ADWAIT) */
#define ADWAIT_WCNT_LSB		0
#define ADWAIT_WCNT_MASK	BITS_H2L(15, ADWAIT_WCNT_LSB)

/* ADC Touch Screen Data Register (ADTCH) */
#define ADTCH_TYPE1		BIT31
#define ADTCH_TYPE0		BIT15

#define ADTCH_DATA1_LSB		16
#define ADTCH_DATA1_MASK	BITS_H2L(27, ADTCH_DATA1_LSB)

#define ADTCH_DATA0_LSB		0
#define ADTCH_DATA0_MASK	BITS_H2L(11, ADTCH_DATA0_LSB)

/* ADC VBAT Date Register (ADVDAT) */
#define ADVDAT_VDATA_LSB	0
#define ADVDAT_VDATA_MASK	BITS_H2L(11, ADVDAT_VDATA_LSB)

/* ADC AUX Data Register (ADADAT) */
#define ADADAT_ADATA_LSB	0
#define ADADAT_ADATA_MASK	BITS_H2L(11, ADADAT_ADATA_LSB)

/*  ADC Clock Divide Register (ADCLK) */
#define ADCLK_CLKDIV_MS_LSB	16
#define ADCLK_CLKDIV_MS_MASK	BITS_H2L(31, ADCLK_CLKDIV_MS_LSB)

#define ADCLK_CLKDIV_US_LSB	8
#define ADCLK_CLKDIV_US_MASK	BITS_H2L(15, ADCLK_CLKDIV_US_LSB)

#define ADCLK_CLKDIV_LSB	0
#define ADCLK_CLKDIV_MASK	BITS_H2L(7, ADCLK_CLKDIV_LSB)

/* ADC Filter Register (ADFLT) */
#define ADFLT_FLT_EN		BIT15

#define ADFLT_FLT_D_LSB		0
#define ADFLT_FLT_D_MASK	BITS_H2L(11, ADFLT_FLT_D_LSB)

#ifndef __MIPS_ASSEMBLER

#define REG_SADC_ADENA          REG8(SADC_ADENA)
#define REG_SADC_ADCFG          REG32(SADC_ADCFG)
#define REG_SADC_ADCTRL         REG8(SADC_ADCTRL)
#define REG_SADC_ADSTATE        REG8(SADC_ADSTATE)
#define REG_SADC_ADSAME         REG16(SADC_ADSAME)
#define REG_SADC_ADWAIT         REG16(SADC_ADWAIT)
#define REG_SADC_ADTCH          REG32(SADC_ADTCH)
#define REG_SADC_ADVDAT         REG16(SADC_ADVDAT)
#define REG_SADC_ADADAT         REG16(SADC_ADADAT)
#define REG_SADC_ADFLT          REG16(SADC_ADFLT)
#define REG_SADC_ADCLK          REG32(SADC_ADCLK)

#endif /* __MIPS_ASSEMBLER */

#define	SCC_BASE	0xB0040000

/*************************************************************************
 * SCC
 *************************************************************************/
#define	SCC_DR			(SCC_BASE + 0x000)
#define	SCC_FDR			(SCC_BASE + 0x004)
#define	SCC_CR			(SCC_BASE + 0x008)
#define	SCC_SR			(SCC_BASE + 0x00C)
#define	SCC_TFR			(SCC_BASE + 0x010)
#define	SCC_EGTR		(SCC_BASE + 0x014)
#define	SCC_ECR			(SCC_BASE + 0x018)
#define	SCC_RTOR		(SCC_BASE + 0x01C)

#define REG_SCC_DR		REG8(SCC_DR)
#define REG_SCC_FDR		REG8(SCC_FDR)
#define REG_SCC_CR		REG32(SCC_CR)
#define REG_SCC_SR		REG16(SCC_SR)
#define REG_SCC_TFR		REG16(SCC_TFR)
#define REG_SCC_EGTR		REG8(SCC_EGTR)
#define REG_SCC_ECR		REG32(SCC_ECR)
#define REG_SCC_RTOR		REG8(SCC_RTOR)

/* SCC FIFO Data Count Register (SCC_FDR) */

#define SCC_FDR_EMPTY		0x00
#define SCC_FDR_FULL		0x10

/* SCC Control Register (SCC_CR) */

#define SCC_CR_SCCE		(1 << 31)
#define SCC_CR_TRS		(1 << 30)
#define SCC_CR_T2R		(1 << 29)
#define SCC_CR_FDIV_BIT		24
#define SCC_CR_FDIV_MASK	(0x3 << SCC_CR_FDIV_BIT)
  #define SCC_CR_FDIV_1		(0 << SCC_CR_FDIV_BIT) /* SCC_CLK frequency is the same as device clock */
  #define SCC_CR_FDIV_2		(1 << SCC_CR_FDIV_BIT) /* SCC_CLK frequency is half of device clock */
#define SCC_CR_FLUSH		(1 << 23)
#define SCC_CR_TRIG_BIT		16
#define SCC_CR_TRIG_MASK	(0x3 << SCC_CR_TRIG_BIT)
  #define SCC_CR_TRIG_1		(0 << SCC_CR_TRIG_BIT) /* Receive/Transmit-FIFO Trigger is 1 */
  #define SCC_CR_TRIG_4		(1 << SCC_CR_TRIG_BIT) /* Receive/Transmit-FIFO Trigger is 4 */
  #define SCC_CR_TRIG_8		(2 << SCC_CR_TRIG_BIT) /* Receive/Transmit-FIFO Trigger is 8 */
  #define SCC_CR_TRIG_14	(3 << SCC_CR_TRIG_BIT) /* Receive/Transmit-FIFO Trigger is 14 */
#define SCC_CR_TP		(1 << 15)
#define SCC_CR_CONV		(1 << 14)
#define SCC_CR_TXIE		(1 << 13)
#define SCC_CR_RXIE		(1 << 12)
#define SCC_CR_TENDIE		(1 << 11)
#define SCC_CR_RTOIE		(1 << 10)
#define SCC_CR_ECIE		(1 << 9)
#define SCC_CR_EPIE		(1 << 8)
#define SCC_CR_RETIE		(1 << 7)
#define SCC_CR_EOIE		(1 << 6)
#define SCC_CR_TSEND		(1 << 3)
#define SCC_CR_PX_BIT		1
#define SCC_CR_PX_MASK		(0x3 << SCC_CR_PX_BIT)
  #define SCC_CR_PX_NOT_SUPPORT	(0 << SCC_CR_PX_BIT) /* SCC does not support clock stop */
  #define SCC_CR_PX_STOP_LOW	(1 << SCC_CR_PX_BIT) /* SCC_CLK stops at state low */
  #define SCC_CR_PX_STOP_HIGH	(2 << SCC_CR_PX_BIT) /* SCC_CLK stops at state high */
#define SCC_CR_CLKSTP		(1 << 0)

/* SCC Status Register (SCC_SR) */

#define SCC_SR_TRANS		(1 << 15)
#define SCC_SR_ORER		(1 << 12)
#define SCC_SR_RTO		(1 << 11)
#define SCC_SR_PER		(1 << 10)
#define SCC_SR_TFTG		(1 << 9)
#define SCC_SR_RFTG		(1 << 8)
#define SCC_SR_TEND		(1 << 7)
#define SCC_SR_RETR_3		(1 << 4)
#define SCC_SR_ECNTO		(1 << 0)

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * SCC
 ***************************************************************************/

#define __scc_enable()			( REG_SCC_CR |= SCC_CR_SCCE )
#define __scc_disable()			( REG_SCC_CR &= ~SCC_CR_SCCE )

#define __scc_set_tx_mode()		( REG_SCC_CR |= SCC_CR_TRS )
#define __scc_set_rx_mode()		( REG_SCC_CR &= ~SCC_CR_TRS )

#define __scc_enable_t2r()		( REG_SCC_CR |= SCC_CR_T2R )
#define __scc_disable_t2r()		( REG_SCC_CR &= ~SCC_CR_T2R )

#define __scc_clk_as_devclk()			\
do {						\
  REG_SCC_CR &= ~SCC_CR_FDIV_MASK;		\
  REG_SCC_CR |= SCC_CR_FDIV_1;			\
} while (0)

#define __scc_clk_as_half_devclk()		\
do {						\
  REG_SCC_CR &= ~SCC_CR_FDIV_MASK;		\
  REG_SCC_CR |= SCC_CR_FDIV_2;			\
} while (0)

/* n=1,4,8,14 */
#define __scc_set_fifo_trigger(n)		\
do {						\
  REG_SCC_CR &= ~SCC_CR_TRIG_MASK;		\
  REG_SCC_CR |= SCC_CR_TRIG_##n;		\
} while (0)

#define __scc_set_protocol(p)			\
do {						\
	if (p)					\
	  	REG_SCC_CR |= SCC_CR_TP;	\
	else					\
	 	REG_SCC_CR &= ~SCC_CR_TP;	\
} while (0)

#define __scc_flush_fifo()		( REG_SCC_CR |= SCC_CR_FLUSH )

#define __scc_set_invert_mode()		( REG_SCC_CR |= SCC_CR_CONV )
#define __scc_set_direct_mode()		( REG_SCC_CR &= ~SCC_CR_CONV )

#define SCC_ERR_INTRS \
    ( SCC_CR_ECIE | SCC_CR_EPIE | SCC_CR_RETIE | SCC_CR_EOIE )
#define SCC_ALL_INTRS \
    ( SCC_CR_TXIE | SCC_CR_RXIE | SCC_CR_TENDIE | SCC_CR_RTOIE | \
      SCC_CR_ECIE | SCC_CR_EPIE | SCC_CR_RETIE | SCC_CR_EOIE )

#define __scc_enable_err_intrs()	( REG_SCC_CR |= SCC_ERR_INTRS )
#define __scc_disable_err_intrs()	( REG_SCC_CR &= ~SCC_ERR_INTRS )

#define SCC_ALL_ERRORS \
    ( SCC_SR_ORER | SCC_SR_RTO | SCC_SR_PER | SCC_SR_RETR_3 | SCC_SR_ECNTO)

#define __scc_clear_errors()		( REG_SCC_SR &= ~SCC_ALL_ERRORS )

#define __scc_enable_all_intrs()	( REG_SCC_CR |= SCC_ALL_INTRS )
#define __scc_disable_all_intrs()	( REG_SCC_CR &= ~SCC_ALL_INTRS )

#define __scc_enable_tx_intr()		( REG_SCC_CR |= SCC_CR_TXIE | SCC_CR_TENDIE )
#define __scc_disable_tx_intr()		( REG_SCC_CR &= ~(SCC_CR_TXIE | SCC_CR_TENDIE) )

#define __scc_enable_rx_intr()		( REG_SCC_CR |= SCC_CR_RXIE)
#define __scc_disable_rx_intr()		( REG_SCC_CR &= ~SCC_CR_RXIE)

#define __scc_set_tsend()		( REG_SCC_CR |= SCC_CR_TSEND )
#define __scc_clear_tsend()		( REG_SCC_CR &= ~SCC_CR_TSEND )

#define __scc_set_clockstop()		( REG_SCC_CR |= SCC_CR_CLKSTP )
#define __scc_clear_clockstop()		( REG_SCC_CR &= ~SCC_CR_CLKSTP )

#define __scc_clockstop_low()			\
do {						\
  REG_SCC_CR &= ~SCC_CR_PX_MASK;		\
  REG_SCC_CR |= SCC_CR_PX_STOP_LOW;		\
} while (0)

#define __scc_clockstop_high()			\
do {						\
  REG_SCC_CR &= ~SCC_CR_PX_MASK;		\
  REG_SCC_CR |= SCC_CR_PX_STOP_HIGH;		\
} while (0)

/* SCC status checking */
#define __scc_check_transfer_status()		( REG_SCC_SR & SCC_SR_TRANS )
#define __scc_check_rx_overrun_error()		( REG_SCC_SR & SCC_SR_ORER )
#define __scc_check_rx_timeout()		( REG_SCC_SR & SCC_SR_RTO )
#define __scc_check_parity_error()		( REG_SCC_SR & SCC_SR_PER )
#define __scc_check_txfifo_trigger()		( REG_SCC_SR & SCC_SR_TFTG )
#define __scc_check_rxfifo_trigger()		( REG_SCC_SR & SCC_SR_RFTG )
#define __scc_check_tx_end()			( REG_SCC_SR & SCC_SR_TEND )
#define __scc_check_retx_3()			( REG_SCC_SR & SCC_SR_RETR_3 )
#define __scc_check_ecnt_overflow()		( REG_SCC_SR & SCC_SR_ECNTO )

#endif /* __MIPS_ASSEMBLER */

#define	SSI0_BASE	0xB0043000
#define	SSI1_BASE	0xB0044000
#define	SSI2_BASE	0xB0045000

/*************************************************************************
 * SSI (Synchronous Serial Interface)
 *************************************************************************/
/* n = 0, 1 (SSI0, SSI1) */
#define	SSI_DR(n)		(SSI0_BASE + 0x000 + (n)*0x1000)
#define	SSI_CR0(n)		(SSI0_BASE + 0x004 + (n)*0x1000)
#define	SSI_CR1(n)		(SSI0_BASE + 0x008 + (n)*0x1000)
#define	SSI_SR(n)		(SSI0_BASE + 0x00C + (n)*0x1000)
#define	SSI_ITR(n)		(SSI0_BASE + 0x010 + (n)*0x1000)
#define	SSI_ICR(n)		(SSI0_BASE + 0x014 + (n)*0x1000)
#define	SSI_GR(n)		(SSI0_BASE + 0x018 + (n)*0x1000)

#define	REG_SSI_DR(n)		REG32(SSI_DR(n))
#define	REG_SSI_CR0(n)		REG16(SSI_CR0(n))
#define	REG_SSI_CR1(n)		REG32(SSI_CR1(n))
#define	REG_SSI_SR(n)		REG32(SSI_SR(n))
#define	REG_SSI_ITR(n)		REG16(SSI_ITR(n))
#define	REG_SSI_ICR(n)		REG8(SSI_ICR(n))
#define	REG_SSI_GR(n)		REG16(SSI_GR(n))

/* SSI Data Register (SSI_DR) */

#define	SSI_DR_GPC_BIT		0
#define	SSI_DR_GPC_MASK		(0x1ff << SSI_DR_GPC_BIT)

#define SSI_MAX_FIFO_ENTRIES 	128 /* 128 txfifo and 128 rxfifo */

/* SSI Control Register 0 (SSI_CR0) */

#define SSI_CR0_SSIE		(1 << 15)
#define SSI_CR0_TIE		(1 << 14)
#define SSI_CR0_RIE		(1 << 13)
#define SSI_CR0_TEIE		(1 << 12)
#define SSI_CR0_REIE		(1 << 11)
#define SSI_CR0_LOOP		(1 << 10)
#define SSI_CR0_RFINE		(1 << 9)
#define SSI_CR0_RFINC		(1 << 8)
#define SSI_CR0_EACLRUN		(1 << 7) /* hardware auto clear underrun when TxFifo no empty */
#define SSI_CR0_FSEL		(1 << 6)
#define SSI_CR0_TFLUSH		(1 << 2)
#define SSI_CR0_RFLUSH		(1 << 1)
#define SSI_CR0_DISREV		(1 << 0)

/* SSI Control Register 1 (SSI_CR1) */

#define SSI_CR1_FRMHL_BIT	30
#define SSI_CR1_FRMHL_MASK	(0x3 << SSI_CR1_FRMHL_BIT)
  #define SSI_CR1_FRMHL_CELOW_CE2LOW	(0 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is low valid and SSI_CE2_ is low valid */
  #define SSI_CR1_FRMHL_CEHIGH_CE2LOW	(1 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is high valid and SSI_CE2_ is low valid */
  #define SSI_CR1_FRMHL_CELOW_CE2HIGH	(2 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is low valid  and SSI_CE2_ is high valid */
  #define SSI_CR1_FRMHL_CEHIGH_CE2HIGH	(3 << SSI_CR1_FRMHL_BIT) /* SSI_CE_ is high valid and SSI_CE2_ is high valid */
#define SSI_CR1_TFVCK_BIT	28
#define SSI_CR1_TFVCK_MASK	(0x3 << SSI_CR1_TFVCK_BIT)
  #define SSI_CR1_TFVCK_0	  (0 << SSI_CR1_TFVCK_BIT)
  #define SSI_CR1_TFVCK_1	  (1 << SSI_CR1_TFVCK_BIT)
  #define SSI_CR1_TFVCK_2	  (2 << SSI_CR1_TFVCK_BIT)
  #define SSI_CR1_TFVCK_3	  (3 << SSI_CR1_TFVCK_BIT)
#define SSI_CR1_TCKFI_BIT	26
#define SSI_CR1_TCKFI_MASK	(0x3 << SSI_CR1_TCKFI_BIT)
  #define SSI_CR1_TCKFI_0	  (0 << SSI_CR1_TCKFI_BIT)
  #define SSI_CR1_TCKFI_1	  (1 << SSI_CR1_TCKFI_BIT)
  #define SSI_CR1_TCKFI_2	  (2 << SSI_CR1_TCKFI_BIT)
  #define SSI_CR1_TCKFI_3	  (3 << SSI_CR1_TCKFI_BIT)
#define SSI_CR1_LFST		(1 << 25)
#define SSI_CR1_ITFRM		(1 << 24)
#define SSI_CR1_UNFIN		(1 << 23)
#define SSI_CR1_MULTS		(1 << 22)
#define SSI_CR1_FMAT_BIT	20
#define SSI_CR1_FMAT_MASK	(0x3 << SSI_CR1_FMAT_BIT)
  #define SSI_CR1_FMAT_SPI	  (0 << SSI_CR1_FMAT_BIT) /* Motorola¡¯s SPI format */
  #define SSI_CR1_FMAT_SSP	  (1 << SSI_CR1_FMAT_BIT) /* TI's SSP format */
  #define SSI_CR1_FMAT_MW1	  (2 << SSI_CR1_FMAT_BIT) /* National Microwire 1 format */
  #define SSI_CR1_FMAT_MW2	  (3 << SSI_CR1_FMAT_BIT) /* National Microwire 2 format */
#define SSI_CR1_TTRG_BIT	16 /* SSI1 TX trigger */
#define SSI_CR1_TTRG_MASK	(0xf << SSI_CR1_TTRG_BIT)
#define SSI_CR1_MCOM_BIT	12
#define SSI_CR1_MCOM_MASK	(0xf << SSI_CR1_MCOM_BIT)
  #define SSI_CR1_MCOM_1BIT	  (0x0 << SSI_CR1_MCOM_BIT) /* 1-bit command selected */
  #define SSI_CR1_MCOM_2BIT	  (0x1 << SSI_CR1_MCOM_BIT) /* 2-bit command selected */
  #define SSI_CR1_MCOM_3BIT	  (0x2 << SSI_CR1_MCOM_BIT) /* 3-bit command selected */
  #define SSI_CR1_MCOM_4BIT	  (0x3 << SSI_CR1_MCOM_BIT) /* 4-bit command selected */
  #define SSI_CR1_MCOM_5BIT	  (0x4 << SSI_CR1_MCOM_BIT) /* 5-bit command selected */
  #define SSI_CR1_MCOM_6BIT	  (0x5 << SSI_CR1_MCOM_BIT) /* 6-bit command selected */
  #define SSI_CR1_MCOM_7BIT	  (0x6 << SSI_CR1_MCOM_BIT) /* 7-bit command selected */
  #define SSI_CR1_MCOM_8BIT	  (0x7 << SSI_CR1_MCOM_BIT) /* 8-bit command selected */
  #define SSI_CR1_MCOM_9BIT	  (0x8 << SSI_CR1_MCOM_BIT) /* 9-bit command selected */
  #define SSI_CR1_MCOM_10BIT	  (0x9 << SSI_CR1_MCOM_BIT) /* 10-bit command selected */
  #define SSI_CR1_MCOM_11BIT	  (0xA << SSI_CR1_MCOM_BIT) /* 11-bit command selected */
  #define SSI_CR1_MCOM_12BIT	  (0xB << SSI_CR1_MCOM_BIT) /* 12-bit command selected */
  #define SSI_CR1_MCOM_13BIT	  (0xC << SSI_CR1_MCOM_BIT) /* 13-bit command selected */
  #define SSI_CR1_MCOM_14BIT	  (0xD << SSI_CR1_MCOM_BIT) /* 14-bit command selected */
  #define SSI_CR1_MCOM_15BIT	  (0xE << SSI_CR1_MCOM_BIT) /* 15-bit command selected */
  #define SSI_CR1_MCOM_16BIT	  (0xF << SSI_CR1_MCOM_BIT) /* 16-bit command selected */
#define SSI_CR1_RTRG_BIT	8 /* SSI RX trigger */
#define SSI_CR1_RTRG_MASK	(0xf << SSI_CR1_RTRG_BIT)
#define SSI_CR1_FLEN_BIT	4
#define SSI_CR1_FLEN_MASK	(0xf << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_2BIT	  (0x0 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_3BIT	  (0x1 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_4BIT	  (0x2 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_5BIT	  (0x3 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_6BIT	  (0x4 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_7BIT	  (0x5 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_8BIT	  (0x6 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_9BIT	  (0x7 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_10BIT	  (0x8 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_11BIT	  (0x9 << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_12BIT	  (0xA << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_13BIT	  (0xB << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_14BIT	  (0xC << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_15BIT	  (0xD << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_16BIT	  (0xE << SSI_CR1_FLEN_BIT)
  #define SSI_CR1_FLEN_17BIT	  (0xF << SSI_CR1_FLEN_BIT)
#define SSI_CR1_PHA		(1 << 1)
#define SSI_CR1_POL		(1 << 0)

/* SSI Status Register (SSI_SR) */

#define SSI_SR_TFIFONUM_BIT	16
#define SSI_SR_TFIFONUM_MASK	(0xff << SSI_SR_TFIFONUM_BIT)
#define SSI_SR_RFIFONUM_BIT	8
#define SSI_SR_RFIFONUM_MASK	(0xff << SSI_SR_RFIFONUM_BIT)
#define SSI_SR_END		(1 << 7)
#define SSI_SR_BUSY		(1 << 6)
#define SSI_SR_TFF		(1 << 5)
#define SSI_SR_RFE		(1 << 4)
#define SSI_SR_TFHE		(1 << 3)
#define SSI_SR_RFHF		(1 << 2)
#define SSI_SR_UNDR		(1 << 1)
#define SSI_SR_OVER		(1 << 0)

/* SSI Interval Time Control Register (SSI_ITR) */

#define	SSI_ITR_CNTCLK		(1 << 15)
#define SSI_ITR_IVLTM_BIT	0
#define SSI_ITR_IVLTM_MASK	(0x7fff << SSI_ITR_IVLTM_BIT)

/*
 * SSI Character-per-frame Control Register (SSI_ICR)
 * SSI_ICR is ignored for SSI_ICR1.FMT != 00b
*/

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * SSI (Synchronous Serial Interface)
 ***************************************************************************/
/* n = 0, 1 (SSI0, SSI1) */
#define __ssi_enable(n) 	( REG_SSI_CR0(n) |= SSI_CR0_SSIE )
#define __ssi_disable(n) 	( REG_SSI_CR0(n) &= ~SSI_CR0_SSIE )
#define __ssi_select_ce(n) 	( REG_SSI_CR0(n) &= ~SSI_CR0_FSEL )

#define __ssi_normal_mode(n) ( REG_SSI_ITR(n) &= ~SSI_ITR_IVLTM_MASK )

#define __ssi_select_ce2(n) 		\
do { 					\
	REG_SSI_CR0(n) |= SSI_CR0_FSEL; 	\
	REG_SSI_CR1(n) &= ~SSI_CR1_MULTS;	\
} while (0)

#define __ssi_select_gpc(n) 			\
do { 						\
	REG_SSI_CR0(n) &= ~SSI_CR0_FSEL;	\
	REG_SSI_CR1(n) |= SSI_CR1_MULTS;	\
} while (0)

#define __ssi_underrun_auto_clear(n) 		\
do { 						\
	REG_SSI_CR0(n) |= SSI_CR0_EACLRUN; 	\
} while (0)

#define __ssi_underrun_clear_manually(n) 	\
do { 						\
	REG_SSI_CR0(n) &= ~SSI_CR0_EACLRUN; 	\
} while (0)

#define __ssi_enable_tx_intr(n)					\
	( REG_SSI_CR0(n) |= SSI_CR0_TIE | SSI_CR0_TEIE )

#define __ssi_disable_tx_intr(n)				\
	( REG_SSI_CR0(n) &= ~(SSI_CR0_TIE | SSI_CR0_TEIE) )

#define __ssi_enable_rx_intr(n)					\
	( REG_SSI_CR0(n) |= SSI_CR0_RIE | SSI_CR0_REIE )

#define __ssi_disable_rx_intr(n)				\
	( REG_SSI_CR0(n) &= ~(SSI_CR0_RIE | SSI_CR0_REIE) )

#define __ssi_enable_txfifo_half_empty_intr(n)  \
	( REG_SSI_CR0(n) |= SSI_CR0_TIE )
#define __ssi_disable_txfifo_half_empty_intr(n)	\
	( REG_SSI_CR0(n) &= ~SSI_CR0_TIE )
#define __ssi_enable_tx_error_intr(n)		\
	( REG_SSI_CR0(n) |= SSI_CR0_TEIE )
#define __ssi_disable_tx_error_intr(n)		\
	( REG_SSI_CR0(n) &= ~SSI_CR0_TEIE )
#define __ssi_enable_rxfifo_half_full_intr(n)	\
	( REG_SSI_CR0(n) |= SSI_CR0_RIE )
#define __ssi_disable_rxfifo_half_full_intr(n)  \
	( REG_SSI_CR0(n) &= ~SSI_CR0_RIE )
#define __ssi_enable_rx_error_intr(n)		\
	( REG_SSI_CR0(n) |= SSI_CR0_REIE )
#define __ssi_disable_rx_error_intr(n)		\
	( REG_SSI_CR0(n) &= ~SSI_CR0_REIE )

#define __ssi_enable_loopback(n)  ( REG_SSI_CR0(n) |= SSI_CR0_LOOP )
#define __ssi_disable_loopback(n) ( REG_SSI_CR0(n) &= ~SSI_CR0_LOOP )

#define __ssi_enable_receive(n)   ( REG_SSI_CR0(n) &= ~SSI_CR0_DISREV )
#define __ssi_disable_receive(n)  ( REG_SSI_CR0(n) |= SSI_CR0_DISREV )

#define __ssi_finish_receive(n)					\
	( REG_SSI_CR0(n) |= (SSI_CR0_RFINE | SSI_CR0_RFINC) )

#define __ssi_disable_recvfinish(n)				\
	( REG_SSI_CR0(n) &= ~(SSI_CR0_RFINE | SSI_CR0_RFINC) )

#define __ssi_flush_txfifo(n)   	( REG_SSI_CR0(n) |= SSI_CR0_TFLUSH )
#define __ssi_flush_rxfifo(n)   	( REG_SSI_CR0(n) |= SSI_CR0_RFLUSH )

#define __ssi_flush_fifo(n)					\
	( REG_SSI_CR0(n) |= SSI_CR0_TFLUSH | SSI_CR0_RFLUSH )

#define __ssi_finish_transmit(n) 	( REG_SSI_CR1(n) &= ~SSI_CR1_UNFIN )
#define __ssi_wait_transmit(n) 		( REG_SSI_CR1(n) |= SSI_CR1_UNFIN )
#define __ssi_use_busy_wait_mode(n) 	__ssi_wait_transmit(n)
#define __ssi_unset_busy_wait_mode(n) 	__ssi_finish_transmit(n)

#define __ssi_spi_format(n)						\
	do {								\
		REG_SSI_CR1(n) &= ~SSI_CR1_FMAT_MASK; 			\
		REG_SSI_CR1(n) |= SSI_CR1_FMAT_SPI;			\
		REG_SSI_CR1(n) &= ~(SSI_CR1_TFVCK_MASK|SSI_CR1_TCKFI_MASK); \
		REG_SSI_CR1(n) |= (SSI_CR1_TFVCK_1 | SSI_CR1_TCKFI_1);	\
	} while (0)

/* TI's SSP format, must clear SSI_CR1.UNFIN */
#define __ssi_ssp_format(n)						\
	do { 								\
		REG_SSI_CR1(n) &= ~(SSI_CR1_FMAT_MASK | SSI_CR1_UNFIN);	\
		REG_SSI_CR1(n) |= SSI_CR1_FMAT_SSP;			\
	} while (0)

/* National's Microwire format, must clear SSI_CR0.RFINE, and set max delay */
#define __ssi_microwire_format(n)					\
	do {								\
		REG_SSI_CR1(n) &= ~SSI_CR1_FMAT_MASK; 			\
		REG_SSI_CR1(n) |= SSI_CR1_FMAT_MW1;			\
		REG_SSI_CR1(n) &= ~(SSI_CR1_TFVCK_MASK|SSI_CR1_TCKFI_MASK); \
		REG_SSI_CR1(n) |= (SSI_CR1_TFVCK_3 | SSI_CR1_TCKFI_3);	\
		REG_SSI_CR0(n) &= ~SSI_CR0_RFINE;			\
	} while (0)

/* CE# level (FRMHL), CE# in interval time (ITFRM),
   clock phase and polarity (PHA POL),
   interval time (SSIITR), interval characters/frame (SSIICR) */

/* frmhl,endian,mcom,flen,pha,pol MASK */
#define SSICR1_MISC_MASK 					\
	( SSI_CR1_FRMHL_MASK | SSI_CR1_LFST | SSI_CR1_MCOM_MASK	\
	  | SSI_CR1_FLEN_MASK | SSI_CR1_PHA | SSI_CR1_POL )

#define __ssi_spi_set_misc(n,frmhl,endian,flen,mcom,pha,pol)		\
	do {								\
		REG_SSI_CR1(n) &= ~SSICR1_MISC_MASK;			\
		REG_SSI_CR1(n) |= ((frmhl) << 30) | ((endian) << 25) | 	\
			(((mcom) - 1) << 12) | (((flen) - 2) << 4) | 	\
			((pha) << 1) | (pol); 				\
	} while(0)

/* Transfer with MSB or LSB first */
#define __ssi_set_msb(n) ( REG_SSI_CR1(n) &= ~SSI_CR1_LFST )
#define __ssi_set_lsb(n) ( REG_SSI_CR1(n) |= SSI_CR1_LFST )

#define __ssi_set_frame_length(n, m)					\
	REG_SSI_CR1(n) = (REG_SSI_CR1(n) & ~SSI_CR1_FLEN_MASK) | (((m) - 2) << 4)

/* m = 1 - 16 */
#define __ssi_set_microwire_command_length(n,m)				\
	( REG_SSI_CR1(n) = ((REG_SSI_CR1(n) & ~SSI_CR1_MCOM_MASK) | SSI_CR1_MCOM_##m##BIT) )

/* Set the clock phase for SPI */
#define __ssi_set_spi_clock_phase(n, m)					\
	( REG_SSI_CR1(n) = ((REG_SSI_CR1(n) & ~SSI_CR1_PHA) | (((m)&0x1)<< 1)))

/* Set the clock polarity for SPI */
#define __ssi_set_spi_clock_polarity(n, p)				\
	( REG_SSI_CR1(n) = ((REG_SSI_CR1(n) & ~SSI_CR1_POL) | ((p)&0x1)) )

/* SSI tx trigger, m = i x 8 */
#define __ssi_set_tx_trigger(n, m)				\
	do {							\
		REG_SSI_CR1(n) &= ~SSI_CR1_TTRG_MASK;		\
		REG_SSI_CR1(n) |= ((m)/8)<<SSI_CR1_TTRG_BIT;	\
	} while (0)

/* SSI rx trigger, m = i x 8 */
#define __ssi_set_rx_trigger(n, m)				\
	do {							\
		REG_SSI_CR1(n) &= ~SSI_CR1_RTRG_MASK;		\
		REG_SSI_CR1(n) |= ((m)/8)<<SSI_CR1_RTRG_BIT;	\
	} while (0)

#define __ssi_get_txfifo_count(n)					\
	( (REG_SSI_SR(n) & SSI_SR_TFIFONUM_MASK) >> SSI_SR_TFIFONUM_BIT )

#define __ssi_get_rxfifo_count(n)					\
	( (REG_SSI_SR(n) & SSI_SR_RFIFONUM_MASK) >> SSI_SR_RFIFONUM_BIT )

#define __ssi_transfer_end(n)		( REG_SSI_SR(n) & SSI_SR_END )
#define __ssi_is_busy(n)		( REG_SSI_SR(n) & SSI_SR_BUSY )

#define __ssi_txfifo_full(n)		( REG_SSI_SR(n) & SSI_SR_TFF )
#define __ssi_rxfifo_empty(n)		( REG_SSI_SR(n) & SSI_SR_RFE )
#define __ssi_rxfifo_half_full(n)	( REG_SSI_SR(n) & SSI_SR_RFHF )
#define __ssi_txfifo_half_empty(n)	( REG_SSI_SR(n) & SSI_SR_TFHE )
#define __ssi_underrun(n)		( REG_SSI_SR(n) & SSI_SR_UNDR )
#define __ssi_overrun(n)		( REG_SSI_SR(n) & SSI_SR_OVER )
#define __ssi_clear_underrun(n)		( REG_SSI_SR(n) = ~SSI_SR_UNDR )
#define __ssi_clear_overrun(n)		( REG_SSI_SR(n) = ~SSI_SR_OVER )
#define __ssi_clear_errors(n)		( REG_SSI_SR(n) &= ~(SSI_SR_UNDR | SSI_SR_OVER) )

#define __ssi_set_clk(n, dev_clk, ssi_clk)			\
	( REG_SSI_GR(n) = (dev_clk) / (2*(ssi_clk)) - 1 )

#define __ssi_receive_data(n) 		REG_SSI_DR(n)
#define __ssi_transmit_data(n, v) 	(REG_SSI_DR(n) = (v))

#endif /* __MIPS_ASSEMBLER */

/*
 * Timer and counter unit module(TCU) address definition
 */
#define	TCU_BASE		0xb0002000

/* TCU group offset */
#define TCU_GOS			0x10

/* TCU total channel number */
#define TCU_CHANNEL_NUM		8

/*
 * TCU registers offset definition
 */
#define TCU_TER_OFFSET		(0x10)  /* r, 16, 0x0000     */
#define TCU_TESR_OFFSET		(0x14)  /* w, 16, 0x????     */
#define TCU_TECR_OFFSET		(0x18)  /* w, 16, 0x????     */

#define TCU_TSR_OFFSET		(0x1c)  /* r, 32, 0x00000000 */
#define TCU_TSSR_OFFSET		(0x2c)  /* w, 32, 0x00000000 */
#define TCU_TSCR_OFFSET		(0x3c)  /* w, 32, 0x0000     */

#define TCU_TFR_OFFSET		(0x20)  /* r, 32, 0x003F003F */
#define TCU_TFSR_OFFSET		(0x24)  /* w, 32, 0x???????? */
#define TCU_TFCR_OFFSET		(0x28)  /* w, 32, 0x???????? */

#define TCU_TMR_OFFSET		(0x30)  /* r, 32, 0x00000000 */
#define TCU_TMSR_OFFSET		(0x34)  /* w, 32, 0x???????? */
#define TCU_TMCR_OFFSET		(0x38)  /* w, 32, 0x???????? */

#define TCU_TSTR_OFFSET		(0xf0)  /* r, 32, 0x00000000 */
#define TCU_TSTSR_OFFSET	(0xf4)  /* w, 32, 0x???????? */
#define TCU_TSTCR_OFFSET	(0xf8)  /* w, 32, 0x???????? */

#define TCU_TDFR_OFFSET		(0x40)  /* rw,16, 0x????     */
#define TCU_TDHR_OFFSET		(0x44)  /* rw,16, 0x????     */
#define TCU_TCNT_OFFSET		(0x48)  /* rw,16, 0x????     */
#define TCU_TCSR_OFFSET		(0x4c)  /* rw,16, 0x0000     */

/*
 * TCU registers address definition
 */
#define TCU_TER		(TCU_BASE + TCU_TER_OFFSET)
#define TCU_TESR	(TCU_BASE + TCU_TESR_OFFSET)
#define TCU_TECR	(TCU_BASE + TCU_TECR_OFFSET)
#define TCU_TSR		(TCU_BASE + TCU_TSR_OFFSET)
#define TCU_TFR		(TCU_BASE + TCU_TFR_OFFSET)
#define TCU_TFSR	(TCU_BASE + TCU_TFSR_OFFSET)
#define TCU_TFCR	(TCU_BASE + TCU_TFCR_OFFSET)
#define TCU_TSSR	(TCU_BASE + TCU_TSSR_OFFSET)
#define TCU_TMR		(TCU_BASE + TCU_TMR_OFFSET)
#define TCU_TMSR	(TCU_BASE + TCU_TMSR_OFFSET)
#define TCU_TMCR	(TCU_BASE + TCU_TMCR_OFFSET)
#define TCU_TSCR	(TCU_BASE + TCU_TSCR_OFFSET)
#define TCU_TSTR	(TCU_BASE + TCU_TSTR_OFFSET)
#define TCU_TSTSR	(TCU_BASE + TCU_TSTSR_OFFSET)
#define TCU_TSTCR	(TCU_BASE + TCU_TSTCR_OFFSET)

/* n is the TCU channel index (0 - 7) */
#define TCU_TDFR(n)	(TCU_BASE + (n) * TCU_GOS + TCU_TDFR_OFFSET)
#define TCU_TDHR(n)	(TCU_BASE + (n) * TCU_GOS + TCU_TDHR_OFFSET)
#define TCU_TCNT(n)	(TCU_BASE + (n) * TCU_GOS + TCU_TCNT_OFFSET)
#define TCU_TCSR(n)	(TCU_BASE + (n) * TCU_GOS + TCU_TCSR_OFFSET)

/*
 * TCU registers bit field common define
 */

/* When n is NOT less than TCU_CHANNEL_NUM, change to TCU_CHANNEL_NUM - 1 */
#define __TIMER(n)	(1 << ((n) < TCU_CHANNEL_NUM ? (n) : (TCU_CHANNEL_NUM - 1))

/* Timer counter enable register(TER) */
#define TER_OSTEN	BIT15
#define TER_TCEN(n)	__TIMER(n)

/* Timer counter enable set register(TESR) */
#define TESR_OST	BIT15
#define TESR_TIMER(n)	__TIMER(n)

/* Timer counter enable clear register(TECR) */
#define TECR_OST	BIT15
#define TECR_TIMER(n)	__TIMER(n)

/* Timer stop register(TSR) */
#define TSR_WDT_STOP		BIT16
#define TSR_OST_STOP		BIT15
#define TSR_TIMER_STOP(n)	__TIMER(n)

/* Timer stop set register(TSSR) */
#define TSSR_WDT		BIT16
#define TSSR_OST		BIT15
#define TSSR_TIMER(n)		__TIMER(n)

/* Timer stop clear register(TSCR) */
#define TSCR_WDT		BIT16
#define TSCR_OST		BIT15
#define TSSR_TIMER(n)		__TIMER(n)

/* Timer flag register(TFR) */
#define TFR_HFLAG(n)	(__TIMER(n) << 16)
#define TFR_OSTFLAG	BIT15
#define TFR_FFLAG(n)	__TIMER(n)

/* Timer flag set register(TFSR) */
#define TFSR_HFLAG(n)	(__TIMER(n) << 16)
#define TFSR_OSTFLAG	BIT15
#define TFSR_FFLAG(n)	__TIMER(n)

/* Timer flag clear register(TFCR) */
#define TFCR_HFLAG(n)	(__TIMER(n) << 16)
#define TFCR_OSTFLAG	BIT15
#define TFCR_FFLAG(n)	(__TIMER(n))

/* Timer mast register(TMR) */
#define TMR_HMASK(n)	(__TIMER(n) << 16)
#define TMR_OSTMASK	BIT15
#define TMR_FMASK(n)	(__TIMER(n))

/* Timer mask set register(TMSR) */
#define TMSR_HMASK(n)	(__TIMER(n) << 16)
#define TMSR_OSTMASK	BIT15
#define TMSR_FMASK(n)	(__TIMER(n))

/* Timer mask clear register(TMCR) */
#define TMCR_HMASK(n)	(__TIMER(n) << 16)
#define TMCR_OSTMASK	BIT15
#define TMCR_FMASK(n)	(__TIMER(n))

/* Timer control register(TCSR) */
#define TCSR_BYPASS		BIT11
#define TCSR_CLRZ		BIT10
#define TCSR_SD_ABRUPT		BIT9
#define TCSR_INITL_HIGH		BIT8
#define TCSR_PWM_EN		BIT7
#define TCSR_PWM_IN_EN		BIT6
#define TCSR_EXT_EN		BIT2
#define TCSR_RTC_EN		BIT1
#define TCSR_PCK_EN		BIT0

#define TCSR_PRESCALE_LSB	3
#define TCSR_PRESCALE_MASK	BITS_H2L(5, TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE1		(0x0 << TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE4		(0x1 << TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE16		(0x2 << TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE64		(0x3 << TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE256	(0x4 << TCSR_PRESCALE_LSB)
#define TCSR_PRESCALE1024	(0x5 << TCSR_PRESCALE_LSB)

/* Timer data full register(TDFR) */
#define TDFR_TDFR_LSB		0
#define TDFR_TDFR_MASK		BITS_H2L(15, TDFR_TDFR_LSB)

/* Timer data half register(TDHR) */
#define TDHR_TDHR_LSB		0
#define TDHR_TDHR_MASK		BITS_H2L(15, TDHR_TDHR_LSB)

/* Timer counter register(TCNT) */
#define TCNT_TCNT_LSB		0
#define TCNT_TCNT_MASK		BITS_H2L(15, TCNT_TCNT_LSB)

/* Timer status register(TSTR) */
#define TSTR_REAL2	BIT18
#define TSTR_REAL1	BIT17
#define TSTR_BUSY2	BIT2
#define TSTR_BUSY1	BIT1

/* Timer status set register(TSTSR) */
#define TSTSR_REALS2	BIT18
#define TSTSR_REALS1	BIT17
#define TSTSR_BUSYS2	BIT2
#define TSTSR_BUSYS1	BIT1

/* Timer status clear register(TSTCR) */
#define TSTCR_REALC2	BIT18
#define TSTCR_REALC1	BIT17
#define TSTCR_BUSYC2	BIT2
#define TSTCR_BUSYC1	BIT1

#ifndef __MIPS_ASSEMBLER

#define REG_TCU_TER	REG16(TCU_TER)
#define REG_TCU_TESR	REG16(TCU_TESR)
#define REG_TCU_TECR	REG16(TCU_TECR)
#define REG_TCU_TSR	REG32(TCU_TSR)
#define REG_TCU_TFR	REG32(TCU_TFR)
#define REG_TCU_TFSR	REG32(TCU_TFSR)
#define REG_TCU_TFCR	REG32(TCU_TFCR)
#define REG_TCU_TSSR	REG32(TCU_TSSR)
#define REG_TCU_TMR	REG32(TCU_TMR)
#define REG_TCU_TMSR	REG32(TCU_TMSR)
#define REG_TCU_TMCR	REG32(TCU_TMCR)
#define REG_TCU_TSCR	REG32(TCU_TSCR)
#define REG_TCU_TSTR	REG32(TCU_TSTR)
#define REG_TCU_TSTSR	REG32(TCU_TSTSR)
#define REG_TCU_TSTCR	REG32(TCU_TSTCR)

#define REG_TCU_TDFR(n)	REG16(TCU_TDFR(n))
#define REG_TCU_TDHR(n)	REG16(TCU_TDHR(n))
#define REG_TCU_TCNT(n)	REG16(TCU_TCNT(n))
#define REG_TCU_TCSR(n)	REG16(TCU_TCSR(n))

// where 'n' is the TCU channel
#define __tcu_select_extalclk(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~(TCSR_EXT_EN | TCSR_RTC_EN | TCSR_PCK_EN)) | TCSR_EXT_EN)
#define __tcu_select_rtcclk(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~(TCSR_EXT_EN | TCSR_RTC_EN | TCSR_PCK_EN)) | TCSR_RTC_EN)
#define __tcu_select_pclk(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~(TCSR_EXT_EN | TCSR_RTC_EN | TCSR_PCK_EN)) | TCSR_PCK_EN)
#define __tcu_disable_pclk(n) \
	REG_TCU_TCSR(n) = (REG_TCU_TCSR((n)) & ~TCSR_PCK_EN);
#define __tcu_select_clk_div1(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE1)
#define __tcu_select_clk_div4(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE4)
#define __tcu_select_clk_div16(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE16)
#define __tcu_select_clk_div64(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE64)
#define __tcu_select_clk_div256(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE256)
#define __tcu_select_clk_div1024(n) \
	(REG_TCU_TCSR((n)) = (REG_TCU_TCSR((n)) & ~TCSR_PRESCALE_MASK) | TCSR_PRESCALE1024)

#define __tcu_enable_pwm_output(n)	(REG_TCU_TCSR((n)) |= TCSR_PWM_EN)
#define __tcu_disable_pwm_output(n)	(REG_TCU_TCSR((n)) &= ~TCSR_PWM_EN)

#define __tcu_init_pwm_output_high(n)	(REG_TCU_TCSR((n)) |= TCSR_INITL_HIGH)
#define __tcu_init_pwm_output_low(n)	(REG_TCU_TCSR((n)) &= ~TCSR_INITL_HIGH)

#define __tcu_set_pwm_output_shutdown_graceful(n)	(REG_TCU_TCSR((n)) &= ~TCSR_SD_ABRUPT)
#define __tcu_set_pwm_output_shutdown_abrupt(n)		(REG_TCU_TCSR((n)) |= TCSR_SD_ABRUPT)

#define __tcu_clear_counter_to_zero(n)	(REG_TCU_TCSR((n)) |= TCSR_CLRZ)

#define __tcu_ost_enabled()		(REG_TCU_TER & TER_OSTEN)
#define __tcu_enable_ost()		(REG_TCU_TESR = TESR_OST)
#define __tcu_disable_ost()		(REG_TCU_TECR = TECR_OST)

#define __tcu_counter_enabled(n)	(REG_TCU_TER & (1 << (n)))
#define __tcu_start_counter(n)		(REG_TCU_TESR |= (1 << (n)))
#define __tcu_stop_counter(n)		(REG_TCU_TECR |= (1 << (n)))

#define __tcu_half_match_flag(n)	(REG_TCU_TFR & (1 << ((n) + 16)))
#define __tcu_full_match_flag(n)	(REG_TCU_TFR & (1 << (n)))
#define __tcu_set_half_match_flag(n)	(REG_TCU_TFSR = (1 << ((n) + 16)))
#define __tcu_set_full_match_flag(n)	(REG_TCU_TFSR = (1 << (n)))
#define __tcu_clear_half_match_flag(n)	(REG_TCU_TFCR = (1 << ((n) + 16)))
#define __tcu_clear_full_match_flag(n)	(REG_TCU_TFCR = (1 << (n)))
#define __tcu_mask_half_match_irq(n)	(REG_TCU_TMSR = (1 << ((n) + 16)))
#define __tcu_mask_full_match_irq(n)	(REG_TCU_TMSR = (1 << (n)))
#define __tcu_unmask_half_match_irq(n)	(REG_TCU_TMCR = (1 << ((n) + 16)))
#define __tcu_unmask_full_match_irq(n)	(REG_TCU_TMCR = (1 << (n)))

#define __tcu_ost_match_flag()		(REG_TCU_TFR & TFR_OSTFLAG)
#define __tcu_set_ost_match_flag()	(REG_TCU_TFSR = TFSR_OSTFLAG)
#define __tcu_clear_ost_match_flag()	(REG_TCU_TFCR = TFCR_OSTFLAG)
#define __tcu_ost_match_irq_masked()	(REG_TCU_TMR & TMR_OSTMASK)
#define __tcu_mask_ost_match_irq()	(REG_TCU_TMSR = TMSR_OSTMASK)
#define __tcu_unmask_ost_match_irq()	(REG_TCU_TMCR = TMCR_OSTMASK)

#define __tcu_wdt_clock_stopped()	(REG_TCU_TSR & TSR_WDT_STOP)
#define __tcu_ost_clock_stopped()	(REG_TCU_TSR & TSR_OST_STOP)
#define __tcu_timer_clock_stopped(n)	(REG_TCU_TSR & (1 << (n)))

#define __tcu_start_wdt_clock()		(REG_TCU_TSCR = TSCR_WDT)
#define __tcu_start_ost_clock()		(REG_TCU_TSCR = TSCR_OST)
#define __tcu_start_timer_clock(n)	(REG_TCU_TSCR = (1 << (n)))

#define __tcu_stop_wdt_clock()		(REG_TCU_TSSR = TSSR_WDT)
#define __tcu_stop_ost_clock()		(REG_TCU_TSSR = TSSR_OST)
#define __tcu_stop_timer_clock(n)	(REG_TCU_TSSR = (1 << (n)))

#define __tcu_get_count(n)		(REG_TCU_TCNT((n)))
#define __tcu_set_count(n,v)		(REG_TCU_TCNT((n)) = (v))
#define __tcu_set_full_data(n,v)	(REG_TCU_TDFR((n)) = (v))
#define __tcu_set_half_data(n,v)	(REG_TCU_TDHR((n)) = (v))

/* TCU2, counter 1, 2*/
#define __tcu_read_real_value(n)	(REG_TCU_TSTR & (1 << ((n) + 16)))
#define __tcu_read_false_value(n)	(REG_TCU_TSTR & (1 << ((n) + 16)))
#define __tcu_counter_busy(n)		(REG_TCU_TSTR & (1 << (n)))
#define __tcu_counter_ready(n)		(REG_TCU_TSTR & (1 << (n)))

#define __tcu_set_read_real_value(n)	(REG_TCU_TSTSR = (1 << ((n) + 16)))
#define __tcu_set_read_false_value(n)	(REG_TCU_TSTCR = (1 << ((n) + 16)))
#define __tcu_set_counter_busy(n)	(REG_TCU_TSTSR = (1 << (n)))
#define __tcu_set_counter_ready(n)	(REG_TCU_TSTCR = (1 << (n)))

#endif /* __MIPS_ASSEMBLER */

#define	TSSI0_BASE	0xB0073000

/*************************************************************************
 * TSSI MPEG 2-TS slave interface
 *************************************************************************/
#define TSSI_ENA       ( TSSI0_BASE + 0x00 )   /* TSSI enable register */
#define TSSI_CFG       ( TSSI0_BASE + 0x04 )   /* TSSI configure register */
#define TSSI_CTRL      ( TSSI0_BASE + 0x08 )   /* TSSI control register */
#define TSSI_STAT      ( TSSI0_BASE + 0x0c )   /* TSSI state register */
#define TSSI_FIFO      ( TSSI0_BASE + 0x10 )   /* TSSI FIFO register */
#define TSSI_PEN       ( TSSI0_BASE + 0x14 )   /* TSSI PID enable register */
#define TSSI_NUM       ( TSSI0_BASE + 0x18 )
#define TSSI_DTR       ( TSSI0_BASE + 0x1c )
#define TSSI_PID(n)    ( TSSI0_BASE + 0x20 + 4*(n) )   /* TSSI PID filter register */
#define TSSI_PID0      ( TSSI0_BASE + 0x20 )
#define TSSI_PID1      ( TSSI0_BASE + 0x24 )
#define TSSI_PID2      ( TSSI0_BASE + 0x28 )
#define TSSI_PID3      ( TSSI0_BASE + 0x2c )
#define TSSI_PID4      ( TSSI0_BASE + 0x30 )
#define TSSI_PID5      ( TSSI0_BASE + 0x34 )
#define TSSI_PID6      ( TSSI0_BASE + 0x38 )
#define TSSI_PID7      ( TSSI0_BASE + 0x3c )
#define TSSI_PID8      ( TSSI0_BASE + 0x40 )
#define TSSI_PID9      ( TSSI0_BASE + 0x44 )
#define TSSI_PID10     ( TSSI0_BASE + 0x48 )
#define TSSI_PID11     ( TSSI0_BASE + 0x4c )
#define TSSI_PID12     ( TSSI0_BASE + 0x50 )
#define TSSI_PID13     ( TSSI0_BASE + 0x54 )
#define TSSI_PID14     ( TSSI0_BASE + 0x58 )
#define TSSI_PID15     ( TSSI0_BASE + 0x5c )
#define TSSI_PID_MAX   16	/* max PID: 15 */

#define TSSI_DDA	( TSSI0_BASE + 0x60 )
#define TSSI_DTA	( TSSI0_BASE + 0x64 )
#define TSSI_DID	( TSSI0_BASE + 0x68 )
#define TSSI_DCMD	( TSSI0_BASE + 0x6c )
#define TSSI_DST	( TSSI0_BASE + 0x70 )
#define TSSI_TC		( TSSI0_BASE + 0x74 )

#define REG_TSSI_ENA       REG8( TSSI_ENA )
#define REG_TSSI_CFG       REG16( TSSI_CFG )
#define REG_TSSI_CTRL      REG8( TSSI_CTRL )
#define REG_TSSI_STAT      REG8( TSSI_STAT )
#define REG_TSSI_FIFO      REG32( TSSI_FIFO )
#define REG_TSSI_PEN       REG32( TSSI_PEN )
#define REG_TSSI_NUM       REG32( TSSI_NUM )
#define REG_TSSI_DTR	   REG32( TSSI_DTR )
#define REG_TSSI_PID(n)    REG32( TSSI_PID(n) )
#define REG_TSSI_PID0      REG32( TSSI_PID0 )
#define REG_TSSI_PID1      REG32( TSSI_PID1 )
#define REG_TSSI_PID2      REG32( TSSI_PID2 )
#define REG_TSSI_PID3      REG32( TSSI_PID3 )
#define REG_TSSI_PID4      REG32( TSSI_PID4 )
#define REG_TSSI_PID5      REG32( TSSI_PID5 )
#define REG_TSSI_PID6      REG32( TSSI_PID6 )
#define REG_TSSI_PID7      REG32( TSSI_PID7 )
#define REG_TSSI_PID8      REG32( TSSI_PID8 )
#define REG_TSSI_PID9      REG32( TSSI_PID9 )
#define REG_TSSI_PID10     REG32( TSSI_PID10 )
#define REG_TSSI_PID11     REG32( TSSI_PID11 )
#define REG_TSSI_PID12     REG32( TSSI_PID12 )
#define REG_TSSI_PID13     REG32( TSSI_PID13 )
#define REG_TSSI_PID14     REG32( TSSI_PID14 )
#define REG_TSSI_PID15     REG32( TSSI_PID15 )

/* TSSI enable register */
#define TSSI_ENA_SFT_RST 	( 1 << 7 )      /* soft reset bit */
#define TSSI_ENA_PID_EN 	( 1 << 2 )      /* soft filtering function enable bit */
#define TSSI_ENA_FAIL		( 1 << 4 )	/* fail signal bit */
#define TSSI_ENA_PEN_0		( 1 << 3 )	/* PID filter enable bit for PID */
#define TSSI_ENA_DMA_EN 	( 1 << 1 )      /* DMA enable bit */
#define TSSI_ENA_ENA 		( 1 << 0 )      /* TSSI enable bit */

/* TSSI configure register */
#define TSSI_CFG_TRIG_BIT 	14 /* fifo trig number */
#define TSSI_CFG_TRIG_MASK 	( 0x7 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_4 	( 0 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_8 	( 1 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_16 	( 2 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_32 	( 3 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_48 	( 4 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_64 	( 5 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_80 	( 6 << TSSI_CFG_TRIG_BIT)
#define TSSI_CFG_TRIG_96 	( 7 << TSSI_CFG_TRIG_BIT)

/* mode of adding data 0 select bit */
#define TSSI_CFG_TRANS_MD_BIT	10
#define TSSI_CFG_TRANS_MD_MASK	( 0x3 << TSSI_CFG_TRANS_MD_BIT)
#define TSSI_CFG_TRANS_MD_0	(0 << TSSI_CFG_TRANS_MD_BIT)
#define TSSI_CFG_TRANS_MD_1	(1 << TSSI_CFG_TRANS_MD_BIT)
#define TSSI_CFG_TRANS_MD_2	(2 << TSSI_CFG_TRANS_MD_BIT)

#define TSSI_CFG_END_WD 	( 1 << 9 )      /* order of data in word */
#define TSSI_CFG_END_BT 	( 1 << 8 )      /* order of data in byte */

#define TSSI_CFG_TSDI_H 	( 1 << 7 )      /* data pin polarity */
#define TSSI_CFG_USE_0 		( 1 << 6 )      /* serial mode data pin select */
#define TSSI_CFG_USE_TSDI0 	( 1 << 6 )      /* TSDI0 as serial mode data pin */
#define TSSI_CFG_USE_TSDI7 	( 0 << 6 )      /* TSDI7 as serial mode data pin */
#define TSSI_CFG_TSCLK_CH 	( 1 << 5 )      /* clk channel select */
#define TSSI_CFG_PARAL 		( 1 << 4 )      /* mode select */
#define TSSI_CFG_PARAL_MODE 	( 1 << 4 )      /* parallel select */
#define TSSI_CFG_SERIAL_MODE 	( 0 << 4 )      /* serial select */
#define TSSI_CFG_TSCLK_P 	( 1 << 3 )      /* clk edge select */
#define TSSI_CFG_TSFRM_H 	( 1 << 2 )      /* TSFRM polarity select */
#define TSSI_CFG_TSSTR_H 	( 1 << 1 )      /* TSSTR polarity select */
#define TSSI_CFG_TSFAIL_H 	( 1 << 0 )      /* TSFAIL polarity select */

/* TSSI control register */
#define TSSI_CTRL_DTRM		( 1 << 2 ) 	/* FIFO data trigger interrupt mask bit */
#define TSSI_CTRL_OVRNM 	( 1 << 1 )      /* FIFO overrun interrupt mask bit */
#define TSSI_CTRL_TRIGM 	( 1 << 0 )      /* FIFO trigger interrupt mask bit */

/* TSSI state register */
#define TSSI_STAT_DTR		( 1 << 2 )	/* FIFO data trigger interrupt flag bit */
#define TSSI_STAT_OVRN 		( 1 << 1 )      /* FIFO overrun interrupt flag bit */
#define TSSI_STAT_TRIG 		( 1 << 0 )      /* FIFO trigger interrupt flag bit */

/* TSSI PID enable register */
#define TSSI_PEN_EN00 	( 1 << 0 )      /* enable PID n */
#define TSSI_PEN_EN10 	( 1 << 1 )
#define TSSI_PEN_EN20 	( 1 << 2 )
#define TSSI_PEN_EN30 	( 1 << 3 )
#define TSSI_PEN_EN40 	( 1 << 4 )
#define TSSI_PEN_EN50 	( 1 << 5 )
#define TSSI_PEN_EN60 	( 1 << 6 )
#define TSSI_PEN_EN70 	( 1 << 7 )
#define TSSI_PEN_EN80 	( 1 << 8 )
#define TSSI_PEN_EN90 	( 1 << 9 )
#define TSSI_PEN_EN100 	( 1 << 10 )
#define TSSI_PEN_EN110 	( 1 << 11 )
#define TSSI_PEN_EN120 	( 1 << 12 )
#define TSSI_PEN_EN130 	( 1 << 13 )
#define TSSI_PEN_EN140 	( 1 << 14 )
#define TSSI_PEN_EN150 	( 1 << 15 )
#define TSSI_PEN_EN01 	( 1 << 16 )
#define TSSI_PEN_EN11 	( 1 << 17 )
#define TSSI_PEN_EN21 	( 1 << 18 )
#define TSSI_PEN_EN31 	( 1 << 19 )
#define TSSI_PEN_EN41 	( 1 << 20 )
#define TSSI_PEN_EN51 	( 1 << 21 )
#define TSSI_PEN_EN61 	( 1 << 22 )
#define TSSI_PEN_EN71 	( 1 << 23 )
#define TSSI_PEN_EN81 	( 1 << 24 )
#define TSSI_PEN_EN91 	( 1 << 25 )
#define TSSI_PEN_EN101 	( 1 << 26 )
#define TSSI_PEN_EN111 	( 1 << 27 )
#define TSSI_PEN_EN121 	( 1 << 28 )
#define TSSI_PEN_EN131 	( 1 << 29 )
#define TSSI_PEN_EN141 	( 1 << 30 )
#define TSSI_PEN_EN151 	( 1 << 31 )
//#define TSSI_PEN_PID0 	( 1 << 31 ) /* PID filter enable PID0 */

/* TSSI Data Number Registers */
#define TSSI_DNUM_BIT	0
#define TSSI_DNUM_MASK	(0x7f << TSSI_DNUM_BIT)

/* TSSI Data Trigger Register */
#define TSSI_DTRG_BIT 	0
#define TSSI_DTRG_MASK	(0x7f << TSSI_DTRG_BIT)

/* TSSI PID Filter Registers */
#define TSSI_PID_PID1_BIT 	16
#define TSSI_PID_PID1_MASK 	(0x1fff<<TSSI_PID_PID1_BIT)
#define TSSI_PID_PID0_BIT 	0
#define TSSI_PID_PID0_MASK 	(0x1fff<<TSSI_PID_PID0_BIT)

/* TSSI DMA Identifier Registers */
#define TSSI_DMA_ID_BIT		0
#define TSSI_DMA_ID_MASK	(0xffff << TSSI_DMA_ID_BIT)

/* TSSI DMA Command Registers */
#define TSSI_DCMD_TLEN_BIT	8
#define TSSI_DCMD_TLEN_MASK	(0xff << TSSI_DCMD_TLEN_BIT)
#define TSSI_DCMD_TEFE		(1 << 4)
#define TSSI_DCMD_TSZ_BIT	2
#define TSSI_DCMD_TSZ_MASK	(0x3 << TSSI_DCMD_TSZ_BIT)
#define TSSI_DCMD_TSZ_4		(0 << TSSI_DCMD_TSZ_BIT)
#define TSSI_DCMD_TSZ_8		(1 << TSSI_DCMD_TSZ_BIT)
#define TSSI_DCMD_TSZ_16	(2 << TSSI_DCMD_TSZ_BIT)
#define TSSI_DCMD_TSZ_32	(3 << TSSI_DCMD_TSZ_BIT)
#define TSSI_DCMD_TEIE		(1 << 1)
#define TSSI_DCMD_LINK		(1 << 0)

/* TSSI DMA Status Registers */
#define TSSI_DST_DID_BIT	16
#define TSSI_DST_DID_MASK	(0xffff << 16)
#define TSSI_DST_TEND		(1 << 0)

/* TSSI Transfer Control Registers */
#define TSSI_TC_OP_BIT		4
#define TSSI_TC_OP_MASK		(0x3 << TSSI_TC_OP_BIT)
//////////////////#define TSSI_TC_OP_0		(
#define TSSI_TC_OPE		(1 << 2)
#define TSSI_TC_EME		(1 << 1)
#define TSSI_TC_APM		(1 << 0)
#ifndef __MIPS_ASSEMBLER

/*************************************************************************
 * TSSI MPEG 2-TS slave interface operation
 *************************************************************************/
#define __tssi_enable()                       ( REG_TSSI_ENA |= TSSI_ENA_ENA )
#define __tssi_disable()                      ( REG_TSSI_ENA &= ~TSSI_ENA_ENA )
#define __tssi_soft_reset()                   ( REG_TSSI_ENA |= TSSI_ENA_SFT_RST )
#define __tssi_filter_enable_pid0()	      ( REG_TSSI_ENA |= TSSI_ENA_PEN_0)
#define __tssi_filter_disable_pid0()	      ( REG_TSSI_ENA &= ~TSSI_ENA_PEN_0)
#define __tssi_dma_enable()                   ( REG_TSSI_ENA |= TSSI_ENA_DMA_EN )
#define __tssi_dma_disable()                  ( REG_TSSI_ENA &= ~TSSI_ENA_DMA_EN )
#define __tssi_filter_enable()                ( REG_TSSI_ENA |= TSSI_ENA_PID_EN )
#define __tssi_filter_disable()               ( REG_TSSI_ENA &= ~TSSI_ENA_PID_EN )

/* n = 4, 8, 16 */
#define __tssi_set_tigger_num(n)			\
	do {						\
		REG_TSSI_CFG &= ~TSSI_CFG_TRIG_MASK;	\
		REG_TSSI_CFG |= TSSI_CFG_TRIG_##n;	\
	} while (0)

#define __tssi_set_data0_mode(n)				\
	do {							\
		REG_TSSI_CFG &= ~ TSSI_CFG_TRANS_MD_MASK;	\
		REG_TSSI_CFG |= TSSI_CFG_TRANS_MD_##n;		\
	}  while(0)

#define __tssi_set_wd_1()                     ( REG_TSSI_CFG |= TSSI_CFG_END_WD )
#define __tssi_set_wd_0()                     ( REG_TSSI_CFG &= ~TSSI_CFG_END_WD )

#define __tssi_set_bt_1()                     ( REG_TSSI_CFG |= TSSI_CFG_END_BD )
#define __tssi_set_bt_0()                     ( REG_TSSI_CFG &= ~TSSI_CFG_END_BD )

#define __tssi_set_data_pola_high()           ( REG_TSSI_CFG |= TSSI_CFG_TSDI_H )
#define __tssi_set_data_pola_low()            ( REG_TSSI_CFG &= ~TSSI_CFG_TSDI_H )

#define __tssi_set_data_use_data0()           ( REG_TSSI_CFG |= TSSI_CFG_USE_0 )
#define __tssi_set_data_use_data7()           ( REG_TSSI_CFG &= ~TSSI_CFG_USE_0 )

#define __tssi_select_clk_fast()              ( REG_TSSI_CFG &= ~TSSI_CFG_TSCLK_CH )
#define __tssi_select_clk_slow()              ( REG_TSSI_CFG |= TSSI_CFG_TSCLK_CH )

#define __tssi_select_serail_mode()           ( REG_TSSI_CFG &= ~TSSI_CFG_PARAL )
#define __tssi_select_paral_mode()            ( REG_TSSI_CFG |= TSSI_CFG_PARAL )

#define __tssi_select_clk_nega_edge()         ( REG_TSSI_CFG &= ~TSSI_CFG_TSCLK_P )
#define __tssi_select_clk_posi_edge()         ( REG_TSSI_CFG |= TSSI_CFG_TSCLK_P )

#define __tssi_select_frm_act_high()          ( REG_TSSI_CFG |= TSSI_CFG_TSFRM_H )
#define __tssi_select_frm_act_low()           ( REG_TSSI_CFG &= ~TSSI_CFG_TSFRM_H )

#define __tssi_select_str_act_high()          ( REG_TSSI_CFG |= TSSI_CFG_TSSTR_H )
#define __tssi_select_str_act_low()           ( REG_TSSI_CFG &= ~TSSI_CFG_TSSTR_H )

#define __tssi_select_fail_act_high()         ( REG_TSSI_CFG |= TSSI_CFG_TSFAIL_H )
#define __tssi_select_fail_act_low()          ( REG_TSSI_CFG &= ~TSSI_CFG_TSFAIL_H )

#define __tssi_enable_data_trigger_irq()      (REG_TSSI_CTRL &= ~TSSI_CTRL_DTRM)
#define __tssi_disable_data_trigger_irq()     (REG_TSSI_CTRL |= TSSI_CTRL_DTRM)

#define __tssi_enable_ovrn_irq()              ( REG_TSSI_CTRL &= ~TSSI_CTRL_OVRNM )
#define __tssi_disable_ovrn_irq()             ( REG_TSSI_CTRL |= TSSI_CTRL_OVRNM )

#define __tssi_enable_trig_irq()              ( REG_TSSI_CTRL &= ~TSSI_CTRL_TRIGM )
#define __tssi_disable_trig_irq()             ( REG_TSSI_CTRL |= TSSI_CTRL_TRIGM )

#define __tssi_state_is_dtr()		      ( REG_TSSI_STAT & TSSI_STAT_DTR )
#define __tssi_state_is_overrun()             ( REG_TSSI_STAT & TSSI_STAT_OVRN )
#define __tssi_state_trigger_meet()           ( REG_TSSI_STAT & TSSI_STAT_TRIG )
#define __tssi_clear_state()                  ( REG_TSSI_STAT = 0 ) /* write 0??? */
#define __tssi_state_clear_overrun()          ( REG_TSSI_STAT = TSSI_STAT_OVRN )   //??????? xyma

//#define __tssi_enable_filte_pid0()            ( REG_TSSI_PEN |= TSSI_PEN_PID0 )
//#define __tssi_disable_filte_pid0()           ( REG_TSSI_PEN &= ~TSSI_PEN_PID0 )

/* m = 0, ..., 31 */
////////////////???????????????????????????????????????????????????????????

#define __tssi_enable_pid_filter(m)				\
	do {							\
		int n = (m);					\
		if ( n>=0 && n <(TSSI_PID_MAX*2) ) {		\
			REG_TSSI_PEN |= ( 1 << n );		\
		}						\
	} while (0)

/* m = 0, ..., 31 */
#define __tssi_disable_pid_filter(m)				       \
	do {							       \
		int n = (m);					       \
		if ( n>=0 && n <(TSSI_PID_MAX*2) ) {		       \
			REG_TSSI_PEN &= ~( 1 << n );		       \
		}						       \
	} while (0)

/* n = 0, ..., 15 */
#define __tssi_set_pid0(n, pid0)					\
	do {								\
		REG_TSSI_PID(n) &= ~TSSI_PID_PID0_MASK;			\
		REG_TSSI_PID(n) |= ((pid0)<<TSSI_PID_PID0_BIT)&TSSI_PID_PID0_MASK; \
	}while (0)
/* n = 0, ..., 15 */
#define __tssi_set_pid1(n, pid1)					\
	do {								\
		REG_TSSI_PID(n) &= ~TSSI_PID_PID1_MASK;			\
		REG_TSSI_PID(n) |= ((pid1)<<TSSI_PID_PID1_BIT)&TSSI_PID_PID1_MASK; \
	}while (0)

/* n = 0, ..., 15 */
#define __tssi_set_pid(n, pid)						\
	do {								\
		if ( n>=0 && n < TSSI_PID_MAX*2) {			\
			if ( n < TSSI_PID_MAX )				\
				__tssi_set_pid0(n, pid);		\
			else						\
				__tssi_set_pid1(n-TSSI_PID_MAX, pid);	\
		}							\
	}while (0)

#endif /* __MIPS_ASSEMBLER */

#define	TVE_BASE	0xB3050100

/*************************************************************************
 * TVE (TV Encoder Controller)
 *************************************************************************/
#define TVE_CTRL	(TVE_BASE + 0x40) /* TV Encoder Control register */
#define TVE_FRCFG	(TVE_BASE + 0x44) /* Frame configure register */
#define TVE_SLCFG1	(TVE_BASE + 0x50) /* TV signal level configure register 1 */
#define TVE_SLCFG2	(TVE_BASE + 0x54) /* TV signal level configure register 2*/
#define TVE_SLCFG3	(TVE_BASE + 0x58) /* TV signal level configure register 3*/
#define TVE_LTCFG1	(TVE_BASE + 0x60) /* Line timing configure register 1 */
#define TVE_LTCFG2	(TVE_BASE + 0x64) /* Line timing configure register 2 */
#define TVE_CFREQ	(TVE_BASE + 0x70) /* Chrominance sub-carrier frequency configure register */
#define TVE_CPHASE	(TVE_BASE + 0x74) /* Chrominance sub-carrier phase configure register */
#define TVE_CBCRCFG	(TVE_BASE + 0x78) /* Chrominance filter configure register */
#define TVE_WSSCR	(TVE_BASE + 0x80) /* Wide screen signal control register */
#define TVE_WSSCFG1	(TVE_BASE + 0x84) /* Wide screen signal configure register 1 */
#define TVE_WSSCFG2	(TVE_BASE + 0x88) /* Wide screen signal configure register 2 */
#define TVE_WSSCFG3	(TVE_BASE + 0x8c) /* Wide screen signal configure register 3 */

#define REG_TVE_CTRL     REG32(TVE_CTRL)
#define REG_TVE_FRCFG    REG32(TVE_FRCFG)
#define REG_TVE_SLCFG1   REG32(TVE_SLCFG1)
#define REG_TVE_SLCFG2   REG32(TVE_SLCFG2)
#define REG_TVE_SLCFG3   REG32(TVE_SLCFG3)
#define REG_TVE_LTCFG1   REG32(TVE_LTCFG1)
#define REG_TVE_LTCFG2   REG32(TVE_LTCFG2)
#define REG_TVE_CFREQ    REG32(TVE_CFREQ)
#define REG_TVE_CPHASE   REG32(TVE_CPHASE)
#define REG_TVE_CBCRCFG	 REG32(TVE_CBCRCFG)
#define REG_TVE_WSSCR    REG32(TVE_WSSCR)
#define REG_TVE_WSSCFG1  REG32(TVE_WSSCFG1)
#define REG_TVE_WSSCFG2	 REG32(TVE_WSSCFG2)
#define REG_TVE_WSSCFG3  REG32(TVE_WSSCFG3)

/* TV Encoder Control register */
#define TVE_CTRL_EYCBCR         (1 << 25)    /* YCbCr_enable */
#define TVE_CTRL_ECVBS          (1 << 24)    /* 1: cvbs_enable 0: s-video*/
#define TVE_CTRL_DAPD3	        (1 << 23)    /* DAC 3 power down */
#define TVE_CTRL_DAPD2	        (1 << 22)    /* DAC 2 power down */
#define TVE_CTRL_DAPD1	        (1 << 21)    /* DAC 1 power down */
#define TVE_CTRL_DAPD           (1 << 20)    /* power down all DACs */
#define TVE_CTRL_YCDLY_BIT      16
#define TVE_CTRL_YCDLY_MASK     (0x7 << TVE_CTRL_YCDLY_BIT)
#define TVE_CTRL_CGAIN_BIT      14
#define TVE_CTRL_CGAIN_MASK     (0x3 << TVE_CTRL_CGAIN_BIT)
  #define TVE_CTRL_CGAIN_FULL		(0 << TVE_CTRL_CGAIN_BIT) /* gain = 1 */
  #define TVE_CTRL_CGAIN_QUTR		(1 << TVE_CTRL_CGAIN_BIT) /* gain = 1/4 */
  #define TVE_CTRL_CGAIN_HALF		(2 << TVE_CTRL_CGAIN_BIT) /* gain = 1/2 */
  #define TVE_CTRL_CGAIN_THREE_QURT	(3 << TVE_CTRL_CGAIN_BIT) /* gain = 3/4 */
#define TVE_CTRL_CBW_BIT        12
#define TVE_CTRL_CBW_MASK       (0x3 << TVE_CTRL_CBW_BIT)
  #define TVE_CTRL_CBW_NARROW	(0 << TVE_CTRL_CBW_BIT) /* Narrow band */
  #define TVE_CTRL_CBW_WIDE	(1 << TVE_CTRL_CBW_BIT) /* Wide band */
  #define TVE_CTRL_CBW_EXTRA	(2 << TVE_CTRL_CBW_BIT) /* Extra wide band */
  #define TVE_CTRL_CBW_ULTRA	(3 << TVE_CTRL_CBW_BIT) /* Ultra wide band */
#define TVE_CTRL_SYNCT          (1 << 9)
#define TVE_CTRL_PAL            (1 << 8) /* 1: PAL, 0: NTSC */
#define TVE_CTRL_FINV           (1 << 7) /* invert_top:1-invert top and bottom fields. */
#define TVE_CTRL_ZBLACK         (1 << 6) /* bypass_yclamp:1-Black of luminance (Y) input is 0.*/
#define TVE_CTRL_CR1ST          (1 << 5) /* uv_order:0-Cb before Cr,1-Cr before Cb */
#define TVE_CTRL_CLBAR          (1 << 4) /* Color bar mode:0-Output input video to TV,1-Output color bar to TV */
#define TVE_CTRL_SWRST          (1 << 0) /* Software reset:1-TVE is reset */

/* Signal level configure register 1 */
#define TVE_SLCFG1_BLACKL_BIT   0
#define TVE_SLCFG1_BLACKL_MASK  (0x3ff << TVE_SLCFG1_BLACKL_BIT)
#define TVE_SLCFG1_WHITEL_BIT   16
#define TVE_SLCFG1_WHITEL_MASK  (0x3ff << TVE_SLCFG1_WHITEL_BIT)

/* Signal level configure register 2 */
#define TVE_SLCFG2_BLANKL_BIT    0
#define TVE_SLCFG2_BLANKL_MASK   (0x3ff << TVE_SLCFG2_BLANKL_BIT)
#define TVE_SLCFG2_VBLANKL_BIT   16
#define TVE_SLCFG2_VBLANKL_MASK  (0x3ff << TVE_SLCFG2_VBLANKL_BIT)

/* Signal level configure register 3 */
#define TVE_SLCFG3_SYNCL_BIT   0
#define TVE_SLCFG3_SYNCL_MASK  (0xff << TVE_SLCFG3_SYNCL_BIT)

/* Line timing configure register 1 */
#define TVE_LTCFG1_BACKP_BIT   0
#define TVE_LTCFG1_BACKP_MASK  (0x7f << TVE_LTCFG1_BACKP_BIT)
#define TVE_LTCFG1_HSYNCW_BIT   8
#define TVE_LTCFG1_HSYNCW_MASK  (0x7f << TVE_LTCFG1_HSYNCW_BIT)
#define TVE_LTCFG1_FRONTP_BIT   16
#define TVE_LTCFG1_FRONTP_MASK  (0x1f << TVE_LTCFG1_FRONTP_BIT)

/* Line timing configure register 2 */
#define TVE_LTCFG2_BURSTW_BIT    0
#define TVE_LTCFG2_BURSTW_MASK   (0x3f << TVE_LTCFG2_BURSTW_BIT)
#define TVE_LTCFG2_PREBW_BIT     8
#define TVE_LTCFG2_PREBW_MASK    (0x1f << TVE_LTCFG2_PREBW_BIT)
#define TVE_LTCFG2_ACTLIN_BIT    16
#define TVE_LTCFG2_ACTLIN_MASK	(0x7ff << TVE_LTCFG2_ACTLIN_BIT)

/* Chrominance sub-carrier phase configure register */
#define TVE_CPHASE_CCRSTP_BIT    0
#define TVE_CPHASE_CCRSTP_MASK   (0x3 << TVE_CPHASE_CCRSTP_BIT)
  #define TVE_CPHASE_CCRSTP_8	(0 << TVE_CPHASE_CCRSTP_BIT) /* Every 8 field */
  #define TVE_CPHASE_CCRSTP_4	(1 << TVE_CPHASE_CCRSTP_BIT) /* Every 4 field */
  #define TVE_CPHASE_CCRSTP_2	(2 << TVE_CPHASE_CCRSTP_BIT) /* Every 2 lines */
  #define TVE_CPHASE_CCRSTP_0	(3 << TVE_CPHASE_CCRSTP_BIT) /* Never */
#define TVE_CPHASE_ACTPH_BIT     16
#define TVE_CPHASE_ACTPH_MASK    (0xff << TVE_CPHASE_ACTPH_BIT)
#define TVE_CPHASE_INITPH_BIT    24
#define TVE_CPHASE_INITPH_MASK   (0xff << TVE_CPHASE_INITPH_BIT)

/* Chrominance filter configure register */
#define TVE_CBCRCFG_CRGAIN_BIT       0
#define TVE_CBCRCFG_CRGAIN_MASK      (0xff << TVE_CBCRCFG_CRGAIN_BIT)
#define TVE_CBCRCFG_CBGAIN_BIT       8
#define TVE_CBCRCFG_CBGAIN_MASK      (0xff << TVE_CBCRCFG_CBGAIN_BIT)
#define TVE_CBCRCFG_CRBA_BIT         16
#define TVE_CBCRCFG_CRBA_MASK        (0xff << TVE_CBCRCFG_CRBA_BIT)
#define TVE_CBCRCFG_CBBA_BIT         24
#define TVE_CBCRCFG_CBBA_MASK        (0xff << TVE_CBCRCFG_CBBA_BIT)

/* Frame configure register */
#define TVE_FRCFG_NLINE_BIT          0
#define TVE_FRCFG_NLINE_MASK         (0x3ff << TVE_FRCFG_NLINE_BIT)
#define TVE_FRCFG_L1ST_BIT           16
#define TVE_FRCFG_L1ST_MASK          (0xff << TVE_FRCFG_L1ST_BIT)

/* Wide screen signal control register */
#define TVE_WSSCR_EWSS0_BIT	0
#define TVE_WSSCR_EWSS1_BIT	1
#define TVE_WSSCR_WSSTP_BIT	2
#define TVE_WSSCR_WSSCKBP_BIT	3
#define TVE_WSSCR_WSSEDGE_BIT	4
#define TVE_WSSCR_WSSEDGE_MASK	(0x7 << TVE_WSSCR_WSSEDGE_BIT)
#define TVE_WSSCR_ENCH_BIT	8
#define TVE_WSSCR_NCHW_BIT	9
#define TVE_WSSCR_NCHFREQ_BIT	12
#define TVE_WSSCR_NCHFREQ_MASK	(0x7 << TVE_WSSCR_NCHFREQ_BIT)

#ifndef __MIPS_ASSEMBLER

/*************************************************************************
 * TVE (TV Encoder Controller) ops
 *************************************************************************/
/* TV Encoder Control register ops */
#define __tve_soft_reset()		(REG_TVE_CTRL |= TVE_CTRL_SWRST)

#define __tve_output_colorbar()		(REG_TVE_CTRL |= TVE_CTRL_CLBAR)
#define __tve_output_video()		(REG_TVE_CTRL &= ~TVE_CTRL_CLBAR)

#define __tve_input_cr_first()		(REG_TVE_CTRL |= TVE_CTRL_CR1ST)
#define __tve_input_cb_first()		(REG_TVE_CTRL &= ~TVE_CTRL_CR1ST)

#define __tve_set_0_as_black()		(REG_TVE_CTRL |= TVE_CTRL_ZBLACK)
#define __tve_set_16_as_black()		(REG_TVE_CTRL &= ~TVE_CTRL_ZBLACK)

#define __tve_ena_invert_top_bottom()	(REG_TVE_CTRL |= TVE_CTRL_FINV)
#define __tve_dis_invert_top_bottom()	(REG_TVE_CTRL &= ~TVE_CTRL_FINV)

#define __tve_set_pal_mode()		(REG_TVE_CTRL |= TVE_CTRL_PAL)
#define __tve_set_ntsc_mode()		(REG_TVE_CTRL &= ~TVE_CTRL_PAL)

#define __tve_set_pal_dura()		(REG_TVE_CTRL |= TVE_CTRL_SYNCT)
#define __tve_set_ntsc_dura()		(REG_TVE_CTRL &= ~TVE_CTRL_SYNCT)

/* n = 0 ~ 3 */
#define __tve_set_c_bandwidth(n) \
do {\
	REG_TVE_CTRL &= ~TVE_CTRL_CBW_MASK;\
	REG_TVE_CTRL |= (n) << TVE_CTRL_CBW_BIT;	\
}while(0)

/* n = 0 ~ 3 */
#define __tve_set_c_gain(n) \
do {\
	REG_TVE_CTRL &= ~TVE_CTRL_CGAIN_MASK;\
	(REG_TVE_CTRL |= (n) << TVE_CTRL_CGAIN_BIT;	\
}while(0)

/* n = 0 ~ 7 */
#define __tve_set_yc_delay(n)				\
do {							\
	REG_TVE_CTRL &= ~TVE_CTRL_YCDLY_MASK		\
	REG_TVE_CTRL |= ((n) << TVE_CTRL_YCDLY_BIT);	\
} while(0)

#define __tve_disable_all_dacs()	(REG_TVE_CTRL |= TVE_CTRL_DAPD)
#define __tve_disable_dac1()		(REG_TVE_CTRL |= TVE_CTRL_DAPD1)
#define __tve_enable_dac1()		(REG_TVE_CTRL &= ~TVE_CTRL_DAPD1)
#define __tve_disable_dac2()		(REG_TVE_CTRL |= TVE_CTRL_DAPD2)
#define __tve_enable_dac2()		(REG_TVE_CTRL &= ~TVE_CTRL_DAPD2)
#define __tve_disable_dac3()		(REG_TVE_CTRL |= TVE_CTRL_DAPD3)
#define __tve_enable_dac3()		(REG_TVE_CTRL &= ~TVE_CTRL_DAPD3)

#define __tve_enable_svideo_fmt()	(REG_TVE_CTRL |= TVE_CTRL_ECVBS)
#define __tve_enable_cvbs_fmt()		(REG_TVE_CTRL &= ~TVE_CTRL_ECVBS)

/* TV Encoder Frame Configure register ops */
/* n = 0 ~ 255 */
#define __tve_set_first_video_line(n)		\
do {\
		REG_TVE_FRCFG &= ~TVE_FRCFG_L1ST_MASK;\
		REG_TVE_FRCFG |= (n) << TVE_FRCFG_L1ST_BIT;\
} while(0)
/* n = 0 ~ 1023 */
#define __tve_set_line_num_per_frm(n)		\
do {\
		REG_TVE_FRCFG &= ~TVE_FRCFG_NLINE_MASK;\
		REG_TVE_CFG |= (n) << TVE_FRCFG_NLINE_BIT;\
} while(0)
#define __tve_get_video_line_num()\
	(((REG_TVE_FRCFG & TVE_FRCFG_NLINE_MASK) >> TVE_FRCFG_NLINE_BIT) - 1 - 2 * ((REG_TVE_FRCFG & TVE_FRCFG_L1ST_MASK) >> TVE_FRCFG_L1ST_BIT))

/* TV Encoder Signal Level Configure register ops */
/* n = 0 ~ 1023 */
#define __tve_set_white_level(n)		\
do {\
		REG_TVE_SLCFG1 &= ~TVE_SLCFG1_WHITEL_MASK;\
		REG_TVE_SLCFG1 |= (n) << TVE_SLCFG1_WHITEL_BIT;\
} while(0)
/* n = 0 ~ 1023 */
#define __tve_set_black_level(n)		\
do {\
		REG_TVE_SLCFG1 &= ~TVE_SLCFG1_BLACKL_MASK;\
		REG_TVE_SLCFG1 |= (n) << TVE_SLCFG1_BLACKL_BIT;\
} while(0)
/* n = 0 ~ 1023 */
#define __tve_set_blank_level(n)		\
do {\
		REG_TVE_SLCFG2 &= ~TVE_SLCFG2_BLANKL_MASK;\
		REG_TVE_SLCFG2 |= (n) << TVE_SLCFG2_BLANKL_BIT;\
} while(0)
/* n = 0 ~ 1023 */
#define __tve_set_vbi_blank_level(n)		\
do {\
		REG_TVE_SLCFG2 &= ~TVE_SLCFG2_VBLANKL_MASK;\
		REG_TVE_SLCFG2 |= (n) << TVE_SLCFG2_VBLANKL_BIT;\
} while(0)
/* n = 0 ~ 1023 */
#define __tve_set_sync_level(n)		\
do {\
		REG_TVE_SLCFG3 &= ~TVE_SLCFG3_SYNCL_MASK;\
		REG_TVE_SLCFG3 |= (n) << TVE_SLCFG3_SYNCL_BIT;\
} while(0)

/* TV Encoder Signal Level Configure register ops */
/* n = 0 ~ 31 */
#define __tve_set_front_porch(n)		\
do {\
		REG_TVE_LTCFG1 &= ~TVE_LTCFG1_FRONTP_MASK;\
		REG_TVE_LTCFG1 |= (n) << TVE_LTCFG1_FRONTP_BIT;	\
} while(0)
/* n = 0 ~ 127 */
#define __tve_set_hsync_width(n)		\
do {\
		REG_TVE_LTCFG1 &= ~TVE_LTCFG1_HSYNCW_MASK;\
		REG_TVE_LTCFG1 |= (n) << TVE_LTCFG1_HSYNCW_BIT;	\
} while(0)
/* n = 0 ~ 127 */
#define __tve_set_back_porch(n)		\
do {\
		REG_TVE_LTCFG1 &= ~TVE_LTCFG1_BACKP_MASK;\
		REG_TVE_LTCFG1 |= (n) << TVE_LTCFG1_BACKP_BIT;	\
} while(0)
/* n = 0 ~ 2047 */
#define __tve_set_active_linec(n)		\
do {\
		REG_TVE_LTCFG2 &= ~TVE_LTCFG2_ACTLIN_MASK;\
		REG_TVE_LTCFG2 |= (n) << TVE_LTCFG2_ACTLIN_BIT;	\
} while(0)
/* n = 0 ~ 31 */
#define __tve_set_breezy_way(n)		\
do {\
		REG_TVE_LTCFG2 &= ~TVE_LTCFG2_PREBW_MASK;\
		REG_TVE_LTCFG2 |= (n) << TVE_LTCFG2_PREBW_BIT;	\
} while(0)

/* n = 0 ~ 127 */
#define __tve_set_burst_width(n)		\
do {\
		REG_TVE_LTCFG2 &= ~TVE_LTCFG2_BURSTW_MASK;\
		REG_TVE_LTCFG2 |= (n) << TVE_LTCFG2_BURSTW_BIT;	\
} while(0)

/* TV Encoder Chrominance filter and Modulation register ops */
/* n = 0 ~ (2^32-1) */
#define __tve_set_c_sub_carrier_freq(n)  REG_TVE_CFREQ = (n)
/* n = 0 ~ 255 */
#define __tve_set_c_sub_carrier_init_phase(n) \
do {   \
	REG_TVE_CPHASE &= ~TVE_CPHASE_INITPH_MASK;	\
	REG_TVE_CPHASE |= (n) << TVE_CPHASE_INITPH_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_c_sub_carrier_act_phase(n) \
do {   \
	REG_TVE_CPHASE &= ~TVE_CPHASE_ACTPH_MASK;	\
	REG_TVE_CPHASE |= (n) << TVE_CPHASE_ACTPH_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_c_phase_rst_period(n) \
do {   \
	REG_TVE_CPHASE &= ~TVE_CPHASE_CCRSTP_MASK;	\
	REG_TVE_CPHASE |= (n) << TVE_CPHASE_CCRSTP_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_cb_burst_amp(n) \
do {   \
	REG_TVE_CBCRCFG &= ~TVE_CBCRCFG_CBBA_MASK;	\
	REG_TVE_CBCRCFG |= (n) << TVE_CBCRCFG_CBBA_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_cr_burst_amp(n) \
do {   \
	REG_TVE_CBCRCFG &= ~TVE_CBCRCFG_CRBA_MASK;	\
	REG_TVE_CBCRCFG |= (n) << TVE_CBCRCFG_CRBA_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_cb_gain_amp(n) \
do {   \
	REG_TVE_CBCRCFG &= ~TVE_CBCRCFG_CBGAIN_MASK;	\
	REG_TVE_CBCRCFG |= (n) << TVE_CBCRCFG_CBGAIN_BIT;	\
} while(0)
/* n = 0 ~ 255 */
#define __tve_set_cr_gain_amp(n) \
do {   \
	REG_TVE_CBCRCFG &= ~TVE_CBCRCFG_CRGAIN_MASK;	\
	REG_TVE_CBCRCFG |= (n) << TVE_CBCRCFG_CRGAIN_BIT;	\
} while(0)

/* TV Encoder Wide Screen Signal Control register ops */
/* n = 0 ~ 7 */
#define __tve_set_notch_freq(n) \
do {   \
	REG_TVE_WSSCR &= ~TVE_WSSCR_NCHFREQ_MASK;	\
	REG_TVE_WSSCR |= (n) << TVE_WSSCR_NCHFREQ_BIT;	\
} while(0)
/* n = 0 ~ 7 */
#define __tve_set_notch_width()	(REG_TVE_WSSCR |= TVE_WSSCR_NCHW_BIT)
#define __tve_clear_notch_width()	(REG_TVE_WSSCR &= ~TVE_WSSCR_NCHW_BIT)
#define __tve_enable_notch()		(REG_TVE_WSSCR |= TVE_WSSCR_ENCH_BIT)
#define __tve_disable_notch()		(REG_TVE_WSSCR &= ~TVE_WSSCR_ENCH_BIT)
/* n = 0 ~ 7 */
#define __tve_set_wss_edge(n) \
do {   \
	REG_TVE_WSSCR &= ~TVE_WSSCR_WSSEDGE_MASK;	\
	REG_TVE_WSSCR |= (n) << TVE_WSSCR_WSSEDGE_BIT;	\
} while(0)
#define __tve_set_wss_clkbyp()		(REG_TVE_WSSCR |= TVE_WSSCR_WSSCKBP_BIT)
#define __tve_set_wss_type()		(REG_TVE_WSSCR |= TVE_WSSCR_WSSTP_BIT)
#define __tve_enable_wssf1()		(REG_TVE_WSSCR |= TVE_WSSCR_EWSS1_BIT)
#define __tve_enable_wssf0()		(REG_TVE_WSSCR |= TVE_WSSCR_EWSS0_BIT)

/* TV Encoder Wide Screen Signal Configure register 1, 2 and 3 ops */
/* n = 0 ~ 1023 */
#define __tve_set_wss_level(n) \
do {   \
	REG_TVE_WSSCFG1 &= ~TVE_WSSCFG1_WSSL_MASK;	\
	REG_TVE_WSSCFG1 |= (n) << TVE_WSSCFG1_WSSL_BIT;	\
} while(0)
/* n = 0 ~ 4095 */
#define __tve_set_wss_freq(n) \
do {   \
	REG_TVE_WSSCFG1 &= ~TVE_WSSCFG1_WSSFREQ_MASK;	\
	REG_TVE_WSSCFG1 |= (n) << TVE_WSSCFG1_WSSFREQ_BIT;	\
} while(0)
/* n = 0, 1; l = 0 ~ 255 */
#define __tve_set_wss_line(n,v)			\
do {   \
	REG_TVE_WSSCFG##n &= ~TVE_WSSCFG_WSSLINE_MASK;	\
	REG_TVE_WSSCFG##n |= (v) << TVE_WSSCFG_WSSLINE_BIT;	\
} while(0)
/* n = 0, 1; d = 0 ~ (2^20-1) */
#define __tve_set_wss_data(n, v)			\
do {   \
	REG_TVE_WSSCFG##n &= ~TVE_WSSCFG_WSSLINE_MASK;	\
	REG_TVE_WSSCFG##n |= (v) << TVE_WSSCFG_WSSLINE_BIT;	\
} while(0)

#endif /* __MIPS_ASSEMBLER */

#define	UART0_BASE	0xB0030000
#define	UART1_BASE	0xB0031000
#define	UART2_BASE	0xB0032000
#define	UART3_BASE	0xB0033000

/*************************************************************************
 * UART
 *************************************************************************/

#define IRDA_BASE	UART0_BASE
#define UART_BASE	UART0_BASE
#define UART_OFF	0x1000

/* Register Offset */
#define OFF_RDR		(0x00)	/* R  8b H'xx */
#define OFF_TDR		(0x00)	/* W  8b H'xx */
#define OFF_DLLR	(0x00)	/* RW 8b H'00 */
#define OFF_DLHR	(0x04)	/* RW 8b H'00 */
#define OFF_IER		(0x04)	/* RW 8b H'00 */
#define OFF_ISR		(0x08)	/* R  8b H'01 */
#define OFF_FCR		(0x08)	/* W  8b H'00 */
#define OFF_LCR		(0x0C)	/* RW 8b H'00 */
#define OFF_MCR		(0x10)	/* RW 8b H'00 */
#define OFF_LSR		(0x14)	/* R  8b H'00 */
#define OFF_MSR		(0x18)	/* R  8b H'00 */
#define OFF_SPR		(0x1C)	/* RW 8b H'00 */
#define OFF_SIRCR	(0x20)	/* RW 8b H'00, UART0 */
#define OFF_UMR		(0x24)	/* RW 8b H'00, UART M Register */
#define OFF_UACR	(0x28)	/* RW 8b H'00, UART Add Cycle Register */

/* Register Address */
#define UART0_RDR	(UART0_BASE + OFF_RDR)
#define UART0_TDR	(UART0_BASE + OFF_TDR)
#define UART0_DLLR	(UART0_BASE + OFF_DLLR)
#define UART0_DLHR	(UART0_BASE + OFF_DLHR)
#define UART0_IER	(UART0_BASE + OFF_IER)
#define UART0_ISR	(UART0_BASE + OFF_ISR)
#define UART0_FCR	(UART0_BASE + OFF_FCR)
#define UART0_LCR	(UART0_BASE + OFF_LCR)
#define UART0_MCR	(UART0_BASE + OFF_MCR)
#define UART0_LSR	(UART0_BASE + OFF_LSR)
#define UART0_MSR	(UART0_BASE + OFF_MSR)
#define UART0_SPR	(UART0_BASE + OFF_SPR)
#define UART0_SIRCR	(UART0_BASE + OFF_SIRCR)
#define UART0_UMR	(UART0_BASE + OFF_UMR)
#define UART0_UACR	(UART0_BASE + OFF_UACR)

#define UART1_RDR	(UART1_BASE + OFF_RDR)
#define UART1_TDR	(UART1_BASE + OFF_TDR)
#define UART1_DLLR	(UART1_BASE + OFF_DLLR)
#define UART1_DLHR	(UART1_BASE + OFF_DLHR)
#define UART1_IER	(UART1_BASE + OFF_IER)
#define UART1_ISR	(UART1_BASE + OFF_ISR)
#define UART1_FCR	(UART1_BASE + OFF_FCR)
#define UART1_LCR	(UART1_BASE + OFF_LCR)
#define UART1_MCR	(UART1_BASE + OFF_MCR)
#define UART1_LSR	(UART1_BASE + OFF_LSR)
#define UART1_MSR	(UART1_BASE + OFF_MSR)
#define UART1_SPR	(UART1_BASE + OFF_SPR)
#define UART1_SIRCR	(UART1_BASE + OFF_SIRCR)

#define UART2_RDR	(UART2_BASE + OFF_RDR)
#define UART2_TDR	(UART2_BASE + OFF_TDR)
#define UART2_DLLR	(UART2_BASE + OFF_DLLR)
#define UART2_DLHR	(UART2_BASE + OFF_DLHR)
#define UART2_IER	(UART2_BASE + OFF_IER)
#define UART2_ISR	(UART2_BASE + OFF_ISR)
#define UART2_FCR	(UART2_BASE + OFF_FCR)
#define UART2_LCR	(UART2_BASE + OFF_LCR)
#define UART2_MCR	(UART2_BASE + OFF_MCR)
#define UART2_LSR	(UART2_BASE + OFF_LSR)
#define UART2_MSR	(UART2_BASE + OFF_MSR)
#define UART2_SPR	(UART2_BASE + OFF_SPR)
#define UART2_SIRCR	(UART2_BASE + OFF_SIRCR)

#define UART3_RDR	(UART3_BASE + OFF_RDR)
#define UART3_TDR	(UART3_BASE + OFF_TDR)
#define UART3_DLLR	(UART3_BASE + OFF_DLLR)
#define UART3_DLHR	(UART3_BASE + OFF_DLHR)
#define UART3_IER	(UART3_BASE + OFF_IER)
#define UART3_ISR	(UART3_BASE + OFF_ISR)
#define UART3_FCR	(UART3_BASE + OFF_FCR)
#define UART3_LCR	(UART3_BASE + OFF_LCR)
#define UART3_MCR	(UART3_BASE + OFF_MCR)
#define UART3_LSR	(UART3_BASE + OFF_LSR)
#define UART3_MSR	(UART3_BASE + OFF_MSR)
#define UART3_SPR	(UART3_BASE + OFF_SPR)
#define UART3_SIRCR	(UART3_BASE + OFF_SIRCR)

/*
 * Define macros for UARTIER
 * UART Interrupt Enable Register
 */
#define UARTIER_RIE	(1 << 0)	/* 0: receive fifo full interrupt disable */
#define UARTIER_TIE	(1 << 1)	/* 0: transmit fifo empty interrupt disable */
#define UARTIER_RLIE	(1 << 2)	/* 0: receive line status interrupt disable */
#define UARTIER_MIE	(1 << 3)	/* 0: modem status interrupt disable */
#define UARTIER_RTIE	(1 << 4)	/* 0: receive timeout interrupt disable */

/*
 * Define macros for UARTISR
 * UART Interrupt Status Register
 */
#define UARTISR_IP	(1 << 0)	/* 0: interrupt is pending  1: no interrupt */
#define UARTISR_IID	(7 << 1)	/* Source of Interrupt */
#define UARTISR_IID_MSI		(0 << 1)  /* Modem status interrupt */
#define UARTISR_IID_THRI	(1 << 1)  /* Transmitter holding register empty */
#define UARTISR_IID_RDI		(2 << 1)  /* Receiver data interrupt */
#define UARTISR_IID_RLSI	(3 << 1)  /* Receiver line status interrupt */
#define UARTISR_IID_RTO		(6 << 1)  /* Receive timeout */
#define UARTISR_FFMS		(3 << 6)  /* FIFO mode select, set when UARTFCR.FE is set to 1 */
#define UARTISR_FFMS_NO_FIFO	(0 << 6)
#define UARTISR_FFMS_FIFO_MODE	(3 << 6)

/*
 * Define macros for UARTFCR
 * UART FIFO Control Register
 */
#define UARTFCR_FE	(1 << 0)	/* 0: non-FIFO mode  1: FIFO mode */
#define UARTFCR_RFLS	(1 << 1)	/* write 1 to flush receive FIFO */
#define UARTFCR_TFLS	(1 << 2)	/* write 1 to flush transmit FIFO */
#define UARTFCR_DMS	(1 << 3)	/* 0: disable DMA mode */
#define UARTFCR_UUE	(1 << 4)	/* 0: disable UART */
#define UARTFCR_RTRG	(3 << 6)	/* Receive FIFO Data Trigger */
#define UARTFCR_RTRG_1	(0 << 6)
#define UARTFCR_RTRG_4	(1 << 6)
#define UARTFCR_RTRG_8	(2 << 6)
#define UARTFCR_RTRG_15	(3 << 6)

/*
 * Define macros for UARTLCR
 * UART Line Control Register
 */
#define UARTLCR_WLEN	(3 << 0)	/* word length */
#define UARTLCR_WLEN_5	(0 << 0)
#define UARTLCR_WLEN_6	(1 << 0)
#define UARTLCR_WLEN_7	(2 << 0)
#define UARTLCR_WLEN_8	(3 << 0)
#define UARTLCR_STOP	(1 << 2)	/* 0: 1 stop bit when word length is 5,6,7,8
					   1: 1.5 stop bits when 5; 2 stop bits when 6,7,8 */
#define UARTLCR_STOP1	(0 << 2)
#define UARTLCR_STOP2	(1 << 2)
#define UARTLCR_PE	(1 << 3)	/* 0: parity disable */
#define UARTLCR_PROE	(1 << 4)	/* 0: even parity  1: odd parity */
#define UARTLCR_SPAR	(1 << 5)	/* 0: sticky parity disable */
#define UARTLCR_SBRK	(1 << 6)	/* write 0 normal, write 1 send break */
#define UARTLCR_DLAB	(1 << 7)	/* 0: access UARTRDR/TDR/IER  1: access UARTDLLR/DLHR */

/*
 * Define macros for UARTLSR
 * UART Line Status Register
 */
#define UARTLSR_DR	(1 << 0)	/* 0: receive FIFO is empty  1: receive data is ready */
#define UARTLSR_ORER	(1 << 1)	/* 0: no overrun error */
#define UARTLSR_PER	(1 << 2)	/* 0: no parity error */
#define UARTLSR_FER	(1 << 3)	/* 0; no framing error */
#define UARTLSR_BRK	(1 << 4)	/* 0: no break detected  1: receive a break signal */
#define UARTLSR_TDRQ	(1 << 5)	/* 1: transmit FIFO half "empty" */
#define UARTLSR_TEMT	(1 << 6)	/* 1: transmit FIFO and shift registers empty */
#define UARTLSR_RFER	(1 << 7)	/* 0: no receive error  1: receive error in FIFO mode */

/*
 * Define macros for UARTMCR
 * UART Modem Control Register
 */
#define UARTMCR_RTS	(1 << 1)	/* 0: RTS_ output high, 1: RTS_ output low */
#define UARTMCR_LOOP	(1 << 4)	/* 0: normal  1: loopback mode */
#define UARTMCR_FCM	(1 << 6)	/* 0: software  1: hardware */
#define UARTMCR_MCE	(1 << 7)	/* 0: modem function is disable */

/*
 * Define macros for UARTMSR
 * UART Modem Status Register
 */
#define UARTMSR_CCTS	(1 << 0)        /* 1: a change on CTS_ pin */
#define UARTMSR_CTS	(1 << 4)	/* 0: CTS_ pin is high */

/*
 * Define macros for SIRCR
 * Slow IrDA Control Register
 */
#define SIRCR_TSIRE	(1 << 0)  /* 0: transmitter is in UART mode  1: SIR mode */
#define SIRCR_RSIRE	(1 << 1)  /* 0: receiver is in UART mode  1: SIR mode */
#define SIRCR_TPWS	(1 << 2)  /* 0: transmit 0 pulse width is 3/16 of bit length
					   1: 0 pulse width is 1.6us for 115.2Kbps */
#define SIRCR_TDPL	(1 << 3)  /* 0: encoder generates a positive pulse for 0 */
#define SIRCR_RDPL	(1 << 4)  /* 0: decoder interprets positive pulse as 0 */

#ifndef __MIPS_ASSEMBLER

/***************************************************************************
 * UART
 ***************************************************************************/
#define __jtag_as_uart3()			\
do {	                    			\
	REG_GPIO_PXSELC(0) = 0x40000000;	\
	REG_GPIO_PXSELS(0) = 0x80000000;	\
} while(0)

#define __uart_enable(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_FCR) |= UARTFCR_UUE | UARTFCR_FE )
#define __uart_disable(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_FCR) = ~UARTFCR_UUE )

#define __uart_enable_transmit_irq(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_IER) |= UARTIER_TIE )
#define __uart_disable_transmit_irq(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_IER) &= ~UARTIER_TIE )

#define __uart_enable_receive_irq(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_IER) |= UARTIER_RIE | UARTIER_RLIE | UARTIER_RTIE )
#define __uart_disable_receive_irq(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_IER) &= ~(UARTIER_RIE | UARTIER_RLIE | UARTIER_RTIE) )

#define __uart_enable_loopback(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_MCR) |= UARTMCR_LOOP )
#define __uart_disable_loopback(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_MCR) &= ~UARTMCR_LOOP )

#define __uart_set_8n1(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_LCR) = UARTLCR_WLEN_8 )

#define __uart_set_baud(n, devclk, baud)						\
  do {											\
	REG8(UART_BASE + UART_OFF*(n) + OFF_LCR) |= UARTLCR_DLAB;			\
	REG8(UART_BASE + UART_OFF*(n) + OFF_DLLR) = (devclk / 16 / baud) & 0xff;	\
	REG8(UART_BASE + UART_OFF*(n) + OFF_DLHR) = ((devclk / 16 / baud) >> 8) & 0xff;	\
	REG8(UART_BASE + UART_OFF*(n) + OFF_LCR) &= ~UARTLCR_DLAB;			\
  } while (0)

#define __uart_parity_error(n) \
  ( (REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) & UARTLSR_PER) != 0 )

#define __uart_clear_errors(n) \
  ( REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) &= ~(UARTLSR_ORER | UARTLSR_BRK | UARTLSR_FER | UARTLSR_PER | UARTLSR_RFER) )

#define __uart_transmit_fifo_empty(n) \
  ( (REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) & UARTLSR_TDRQ) != 0 )

#define __uart_transmit_end(n) \
  ( (REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) & UARTLSR_TEMT) != 0 )

#define __uart_transmit_char(n, ch) \
  REG8(UART_BASE + UART_OFF*(n) + OFF_TDR) = (ch)

#define __uart_receive_fifo_full(n) \
  ( (REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) & UARTLSR_DR) != 0 )

#define __uart_receive_ready(n) \
  ( (REG8(UART_BASE + UART_OFF*(n) + OFF_LSR) & UARTLSR_DR) != 0 )

#define __uart_receive_char(n) \
  REG8(UART_BASE + UART_OFF*(n) + OFF_RDR)

#define __uart_disable_irda() \
  ( REG8(IRDA_BASE + OFF_SIRCR) &= ~(SIRCR_TSIRE | SIRCR_RSIRE) )
#define __uart_enable_irda() \
  /* Tx high pulse as 0, Rx low pulse as 0 */ \
  ( REG8(IRDA_BASE + OFF_SIRCR) = SIRCR_TSIRE | SIRCR_RSIRE | SIRCR_RXPL | SIRCR_TPWS )

#endif /* __MIPS_ASSEMBLER */

/*
 * Watchdog timer module(WDT) address definition
 */
#define	WDT_BASE	0xb0002000

/*
 * WDT registers offset address definition
 */
#define WDT_WDR_OFFSET		(0x00)	/* rw, 16, 0x???? */
#define WDT_WCER_OFFSET		(0x04)	/* rw,  8, 0x00 */
#define WDT_WCNT_OFFSET		(0x08)	/* rw, 16, 0x???? */
#define WDT_WCSR_OFFSET		(0x0c)	/* rw, 16, 0x0000 */

/*
 * WDT registers address definition
 */
#define WDT_WDR		(WDT_BASE + WDT_WDR_OFFSET)
#define WDT_WCER	(WDT_BASE + WDT_WCER_OFFSET)
#define WDT_WCNT	(WDT_BASE + WDT_WCNT_OFFSET)
#define WDT_WCSR	(WDT_BASE + WDT_WCSR_OFFSET)

/*
 * WDT registers common define
 */

/* Watchdog counter enable register(WCER) */
#define WCER_TCEN	BIT0

/* Watchdog control register(WCSR) */
#define WCSR_PRESCALE_LSB	3
#define WCSR_PRESCALE_MASK	BITS_H2L(5, WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE1		(0x0 << WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE4		(0x1 << WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE16		(0x2 << WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE64		(0x3 << WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE256	(0x4 << WCSR_PRESCALE_LSB)
#define WCSR_PRESCALE1024	(0x5 << WCSR_PRESCALE_LSB)

#define WCSR_CLKIN_LSB		0
#define WCSR_CLKIN_MASK		BITS_H2L(2, WCSR_CLKIN_LSB)
#define WCSR_CLKIN_PCK		(0x1 << WCSR_CLKIN_LSB)
#define WCSR_CLKIN_RTC		(0x2 << WCSR_CLKIN_LSB)
#define WCSR_CLKIN_EXT		(0x4 << WCSR_CLKIN_LSB)

#ifndef __MIPS_ASSEMBLER

#define REG_WDT_WDR	REG16(WDT_WDR)
#define REG_WDT_WCER	REG8(WDT_WCER)
#define REG_WDT_WCNT	REG16(WDT_WCNT)
#define REG_WDT_WCSR	REG16(WDT_WCSR)

#endif /* __MIPS_ASSEMBLER */

/*
 * Operating system timer module(OST) address definition
 */
#define	OST_BASE	0xb0002000

/*
 * OST registers offset address definition
 */
#define OST_OSTDR_OFFSET	(0xe0)  /* rw, 32, 0x???????? */
#define OST_OSTCNT_OFFSET	(0xe4)  /* rw, 32, 0x???????? */
#define OST_OSTCNTL_OFFSET	(0xe4)  /* rw, 32, 0x???????? */
#define OST_OSTCNTH_OFFSET	(0xe8)  /* rw, 32, 0x???????? */
#define OST_OSTCSR_OFFSET	(0xec)  /* rw, 16, 0x0000 */
#define OST_OSTCNTHBUF_OFFSET	(0xfc)	/*  r, 32, 0x???????? */

/*
 * OST registers address definition
 */
#define OST_OSTDR	(OST_BASE + OST_OSTDR_OFFSET)
#define OST_OSTCNT	(OST_BASE + OST_OSTCNT_OFFSET)
#define OST_OSTCNTL	(OST_BASE + OST_OSTCNTL_OFFSET)
#define OST_OSTCNTH	(OST_BASE + OST_OSTCNTH_OFFSET)
#define OST_OSTCSR	(OST_BASE + OST_OSTCSR_OFFSET)
#define OST_OSTCNTHBUF	(OST_BASE + OST_OSTCNTHBUF_OFFSET)

/*
 * OST registers common define
 */

/* Operating system control register(OSTCSR) */
#define OSTCSR_CNT_MD		BIT15
#define OSTCSR_SD		BIT9
#define OSTCSR_EXT_EN		BIT2
#define OSTCSR_RTC_EN		BIT1
#define OSTCSR_PCK_EN		BIT0

#define OSTCSR_PRESCALE_LSB	3
#define OSTCSR_PRESCALE_MASK	BITS_H2L(5, OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE1	(0x0 << OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE4	(0x1 << OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE16	(0x2 << OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE64	(0x3 << OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE256	(0x4 << OSTCSR_PRESCALE_LSB)
#define OSTCSR_PRESCALE1024	(0x5 << OSTCSR_PRESCALE_LSB)

#ifndef __MIPS_ASSEMBLER

#define REG_OST_OSTDR		REG32(OST_OSTDR)
#define REG_OST_OSTCNT		REG32(OST_OSTCNT)
#define REG_OST_OSTCNTL		REG32(OST_OSTCNTL)
#define REG_OST_OSTCNTH		REG32(OST_OSTCNTH)
#define REG_OST_OSTCSR		REG16(OST_OSTCSR)
#define REG_OST_OSTCNTHBUF	REG32(OST_OSTCNTHBUF)

#endif /* __MIPS_ASSEMBLER */

#define AOSD_BASE        0xB3070000

/*************************************************************************
 * OSD (On Screen Display)
 *************************************************************************/
#define AOSD_ADDR0             (AOSD_BASE + 0x00)
#define AOSD_ADDR1             (AOSD_BASE + 0x04)
#define AOSD_ADDR2             (AOSD_BASE + 0x08)
#define AOSD_ADDR3             (AOSD_BASE + 0x0C)
#define AOSD_WADDR             (AOSD_BASE + 0x10)
#define AOSD_ADDRLEN           (AOSD_BASE + 0x14)
#define AOSD_ALPHA_VALUE       (AOSD_BASE + 0x18)
#define AOSD_CTRL              (AOSD_BASE + 0x1C)
#define AOSD_INT               (AOSD_BASE + 0x20)

#define REG_AOSD_ADDR0         REG32(AOSD_ADDR0)
#define REG_AOSD_ADDR1         REG32(AOSD_ADDR1)
#define REG_AOSD_ADDR2         REG32(AOSD_ADDR2)
#define REG_AOSD_ADDR3         REG32(AOSD_ADDR3)
#define REG_AOSD_WADDR         REG32(AOSD_WADDR)
#define REG_AOSD_ADDRLEN       REG32(AOSD_ADDRLEN)
#define REG_AOSD_ALPHA_VALUE   REG32(AOSD_ALPHA_VALUE)
#define REG_AOSD_CTRL          REG32(AOSD_CTRL)
#define REG_AOSD_INT           REG32(AOSD_INT)

#define AOSD_CTRL_FRMLV_MASK        (0x3 << 18)
#define AOSD_CTRL_FRMLV_2        (0x1 << 18)
#define AOSD_CTRL_FRMLV_3        (0x2 << 18)
#define AOSD_CTRL_FRMLV_4        (0x3 << 18)

#define AOSD_CTRL_FRM_END        (1 << 17)
#define AOSD_CTRL_ALPHA_START    (1 << 16)
#define AOSD_CTRL_INT_MAKS            (1 << 15)
#define AOSD_CTRL_CHANNEL_LEVEL_BIT    7
#define AOSD_CTRL_CHANNEL_LEVEL_MASK   (0xff <<  AOSD_CTRL_CHANNEL_LEVEL_BIT)
#define AOSD_CTRL_ALPHA_MODE_BIT       3
#define AOSD_CTRL_ALPHA_MODE_MASK      (0xf << AOSD_CTRL_ALPHA_MODE_BIT)
#define AOSD_CTRL_ALPHA_PIXEL_MODE     0
#define AOSD_CTRL_ALPHA_FRAME_MODE     1

#define AOSD_CTRL_FORMAT_MODE_BIT     1
#define AOSD_CTRL_FORMAT_MODE_MASK     (0x3 << 1)
#define AOSD_CTRL_RGB565_FORMAT_MODE   (0 << AOSD_CTRL_FORMAT_MODE_BIT)
#define AOSD_CTRL_RGB555_FORMAT_MODE   (1 << AOSD_CTRL_FORMAT_MODE_BIT)
#define AOSD_CTRL_RGB8888_FORMAT_MODE  (2 << AOSD_CTRL_FORMAT_MODE_BIT)

#define AOSD_ALPHA_ENABLE        (1 << 0)

#define AOSD_INT_COMPRESS_END   (1 << 1)
#define AOSD_INT_AOSD_END        (1 << 0)

#define __osd_enable_alpha() 	(REG_AOSD_CTRL |= AOSD_ALPHA_ENABLE)
#define __osd_alpha_start()     (REG_AOSD_CTRL |= AOSD_CTRL_ALPHA_START)

/*************************************************************************
 * COMPRESS
 *************************************************************************/
#define COMPRESS_SCR_ADDR      (AOSD_BASE + 0x00)
#define COMPRESS_DES_ADDR      (AOSD_BASE + 0x10)
#define COMPRESS_DST_OFFSET       (AOSD_BASE + 0x34)
#define COMPRESS_FRAME_SIZE       (AOSD_BASE + 0x38)
#define COMPRESS_CTRL       (AOSD_BASE + 0x3C)
#define COMPRESS_RATIO      (AOSD_BASE + 0x40)
#define COMPRESS_SRC_OFFSET     (AOSD_BASE + 0x44)

#define REG_COMPRESS_SCR_ADDR      REG32(COMPRESS_SCR_ADDR)
#define REG_COMPRESS_DES_ADDR      REG32(COMPRESS_DES_ADDR)
#define REG_COMPRESS_DST_OFFSET       REG32(COMPRESS_DST_OFFSET)
#define REG_COMPRESS_FRAME_SIZE       REG32(COMPRESS_FRAME_SIZE)
#define REG_COMPRESS_CTRL       REG32(COMPRESS_CTRL)
#define REG_COMPRESS_RATIO      REG32(COMPRESS_RATIO)
#define REG_COMPRESS_SRC_OFFSET     REG32(COMPRESS_SRC_OFFSET)

#define COMPRESS_CTRL_WITHOUT_ALPHA     (1 << 4)
#define COMPRESS_CTRL_WITH_ALPHA     (0 << 4)
#define COMPRESS_CTRL_COMP_START   (1 << 3)
#define COMPRESS_CTRL_COMP_END     (1 << 2)
#define COMPRESS_CTRL_INT_MASK     (1 << 1)
#define COMPRESS_CTRL_COMP_ENABLE  (1 << 0)

#define COMPRESS_RATIO_FRM_BYPASS   (1 << 31)
#define COMPRESS_BYPASS_ROW         (1 << 12)
#define COMPRESS_ROW_QUARTER        (1 << 0)

#define COMPRESS_CTRL_ALIGNED_MODE_BIT	(31)
#define COMPRESS_CTRL_ALIGNED_16_WORD	(0 << COMPRESS_CTRL_ALIGNED_MODE_BIT)
#define COMPRESS_CTRL_ALIGNED_64_WORD	(1 << COMPRESS_CTRL_ALIGNED_MODE_BIT)

#define __compress_enable()       (REG_COMPRESS_CTRL |= COMPRESS_INT_AOSD_END)
#define __compress_start()        (REG_COMPRESS_CTRL |= COMPRESS_CTRL_COMP_START)
#define __compress_with_alpha()   (REG_COMPRESS_CTRL |= COMPRESS_CTRL_ALPHA_EN)

/* Rockbox defines */

/* Timer frequency */
#define TIMER_FREQ (CFG_EXTAL) /* For full precision! */

/* Serial */
#define CFG_UART_BASE UART1_BASE /* Base of the UART channel */
#define CFG_BAUDRATE  57600

#endif /* __JZ4760B_H__ */