bcp_usb.c
88.2 KB
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
/*************************************************************************
*
* File: bcp_usb.h
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/times.h>
#include <netinet/in.h>
#include <PR/ultratypes.h>
#include <time.h>
#include "PR/bcp.h"
#include "simipc.h"
#include "iomap.h"
#include "bcptest.h"
#include "bcp_util.h"
#include "simipc.h"
#include "trace.h"
#include "bcp_usb.h"
BufferDescriptorTableType usb1_bdt, usb0_bdt;
/*
Read usb register and compare with expect value
if something wrong return -1 else return 0
which_usb ---- which usb device is under test
usb0_reg ---- which usb register offset as USB0
expect ---- expect value
*/
int usb_read_reg_test(int which_usb, int usb0_reg, int expect, int extra)
{
unsigned int base_addr, ret, addr;
base_addr = (which_usb)? USB1_BASE_REG : USB0_BASE_REG;
addr = base_addr + (usb0_reg - USB0_BASE_REG);
ret = IO_READ(addr);
_TRACE(DALL, fprintf(LogFp, "\n\tRead: usb register=%x ret=%x",
addr, ret));
if (ret != expect) {
_TRACE(DERROR, LOG_ERROR(expect, ret));
return -1;
}
return 0;
}
/* Test USB register when it reset
which_usb ---- which USB device
return 0 if succeed
*/
int usb_reset_test(int which_usb, int already_sof, int a3, int a4)
{
unsigned int err=0, otg_status;
_TRACE(DALL, fprintf(LogFp, "\n\t Reset test on usb device %s",
(which_usb)?"USB device 2":"USB device 1"));
err -= usb_read_reg_test(which_usb, USB0_PER_ID_REG, 0x04, 0);
err -= usb_read_reg_test(which_usb, USB0_ID_COMP_REG, 0xfb, 0);
err -= usb_read_reg_test(which_usb, USB0_OTG_INT_EN_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_OTG_CTRL_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_INT_ENB_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ERR_STAT_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ERR_ENB_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ADDR_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_BDT_PAGE_01_REG, 0, 0);
if (!already_sof)
err -= usb_read_reg_test(which_usb, USB0_FRM_NUML_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_FRM_NUMH_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_TOKEN_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_SOFTHLDL_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_BDT_PAGE_02_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_BDT_PAGE_03_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT0_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT1_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT2_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT3_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT4_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT5_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT6_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT7_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT8_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT9_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT10_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT11_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT12_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT13_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT14_REG, 0, 0);
err -= usb_read_reg_test(which_usb, USB0_ENDPT15_REG, 0, 0);
/* otg_status = IO_READ(USB_REG_ADDR(which_usb, USB0_OTG_STATUS_REG));
if ((otg_status & 0x80) != 0x80) {
_TRACE(DERROR, fprintf(LogFp, "\n\t Default should be slave"));
return -1;
} */
return (err==0?0:-1);
}
/* Write data into usb device "which" reg
return 0 (succeed)
*/
int usb_reg_write_test(int which, int reg, int data, int extra)
{
unsigned int base_addr, addr;
base_addr = (which)?USB1_BASE_REG:USB0_BASE_REG;
addr = base_addr + (reg - USB0_BASE_REG);
IO_WRITE(addr, data);
_TRACE(DALL, fprintf(LogFp, "\n\twrite: usb register=%x data=%x",
addr, data));
return 0;
}
/* Test USB register read and write
return 0 if succeed
*/
int usb_reg_rw(int which, int reg, int data, int mask)
{
int test_data[4] = { 0xff, 0xaa, 0x55, 0x00};
int test_reg[9] = {
USB0_ERR_ENB_REG,
USB0_ADDR_REG,
USB0_BDT_PAGE_01_REG,
USB0_BDT_PAGE_02_REG,
USB0_BDT_PAGE_03_REG,
USB0_ENDPT0_REG,
USB0_ENDPT1_REG,
USB0_ENDPT2_REG,
USB0_ENDPT3_REG
};
int test_mask[9]={0xff, 0xfe, 0xff, 0xff, 0xff,
0x1f, 0x1f, 0x1f, 0x1f};
int i, j, err=0, v;
for (i=0; i<sizeof(test_data)/sizeof(int); i++)
for (j=0; j<sizeof(test_reg)/sizeof(int); j++) {
v = test_data[i] & test_mask[j];
usb_reg_write_test(which, test_reg[j], v, 0);
err -= usb_read_reg_test(which, test_reg[j], v, 0);
}
return (err == 0?0:-1);
}
/* Set bdt address */
int set_bdt_addr(int which, int addr)
{
addr &= 0xfffffe00;
usb_reg_write_test(which, USB0_BDT_PAGE_01_REG,
(addr & 0x0000ff00)>>8, 0);
usb_reg_write_test(which, USB0_BDT_PAGE_02_REG,
(addr & 0x00ff0000)>>16, 0);
usb_reg_write_test(which, USB0_BDT_PAGE_03_REG,
(addr >> 24) & 0xff, 0);
return 0;
}
/* Write given endpnt bdt data to hardware*/
int usb_write_bdt_data(int which,
BufferDescriptorTableType *bd_table_ptr, int endpt)
{
int i, *p_bdt, addr;
if (bd_table_ptr == NULL) return -1;
p_bdt = (int *) bd_table_ptr[endpt];
addr = (which)?USB1_BDT_ADDR:USB0_BDT_ADDR;
for (i=0; i<sizeof(BufferDescriptor); i++) {
IO_WRITE(addr+(i<<2), ENDIAN_SWAP(p_bdt[i]));
}
return 0;
}
/* Write single entry to BDT table
The BDT table can be anywhere (i.e can be in memory) */
int usb_write_bdt_entry(
int bdt_addr, /* BDT addr in system */
BufferDescriptorTableType *bd_table_ptr, /* For test */
int endpt, /* Which end point */
int snd, /* out direction */
int odd) /* odd entry */
{
int offset;
int *p_bdt;
offset = (endpt*4 + (snd?1:0)*2 + (odd?1:0)) * BCP_USB_BDT_SIZE;
if (bd_table_ptr == NULL) return -1;
p_bdt = ((int *) (*bd_table_ptr)) + (offset>>2);
IO_WRITE(bdt_addr + offset, ENDIAN_SWAP(p_bdt[0]));
IO_WRITE(bdt_addr + offset + 4, ENDIAN_SWAP(p_bdt[1]));
return 0;
}
/* Read single entry from BDT table */
int usb_read_bdt_entry(
int bdt_addr, /* BDT addr in system */
BufferDescriptorTableType *bd_table_ptr, /* For test */
int endpt, /* Which end point */
int snd, /* out direction */
int odd) /* odd entry */
{
int *p_bdt, offset;
if (bd_table_ptr == NULL) return -1;
offset = (endpt*4 + (snd?1:0)*2 + (odd?1:0)) * BCP_USB_BDT_SIZE;
p_bdt = ((int *) (*bd_table_ptr)) + (offset>>2);
p_bdt[0] = IO_READ(bdt_addr + offset);
p_bdt[0] = ENDIAN_SWAP(p_bdt[0]);
p_bdt[1] = IO_READ(bdt_addr + offset +4);
p_bdt[1] = ENDIAN_SWAP(p_bdt[1]);
return 0;
}
/* read given endpnt bdt data from verilog */
int usb_read_bdt_data(int which,
BufferDescriptorTableType *bd_table_ptr, int endpt)
{
int i, *p_bdt, addr;
if (bd_table_ptr == NULL) return -1;
p_bdt = (int *) bd_table_ptr[endpt];
addr = (which)?USB1_BDT_ADDR:USB0_BDT_ADDR;
for (i=0; i<sizeof(BufferDescriptor); i++) {
p_bdt[i] = IO_READ(addr+(i<<2));
p_bdt[i] = ENDIAN_SWAP(p_bdt[i]);
}
return 0;
}
/* setup device */
int usb_device_setup(int which)
{
int setup_reg[] = {
USB0_OTG_INT_EN_REG,
//USB0_OTG_CTRL_REG,
USB0_CTL_REG,
USB0_INT_ENB_REG,
USB0_ERR_ENB_REG,
USB0_ADDR_REG,
USB0_FRM_NUML_REG,
USB0_FRM_NUMH_REG
};
BufferDescriptorTableType *bd_table_ptr;
int i;
set_bdt_addr(which, (which)?USB1_BDT_ADDR:USB0_BDT_ADDR);
for (i=0; i<sizeof(setup_reg)/sizeof(int); i++)
usb_reg_write_test(which, setup_reg[i], 0, 0);
for (i=0; i<64; i+=4)
usb_reg_write_test(which, USB0_ENDPT0_REG+i, 0, 0);
for (i=0; i<16; i+=4)
usb_reg_write_test(which, USB0_ENDPT0_REG+i, 0, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x01, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x02, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x80, 0);
bd_table_ptr = (which)?(&usb1_bdt):(&usb0_bdt);
// BDT for the first recieved packet
(*bd_table_ptr)[0][0][0].rsrvd1_0 = 0;
(*bd_table_ptr)[0][0][0].buffer_addr = (which)?USB1_RX_BUF1:USB0_RX_BUF1;
(*bd_table_ptr)[0][0][0].rsrvd15_8 = 0;
(*bd_table_ptr)[0][0][0].rsrvd31_26 = 0;
(*bd_table_ptr)[0][0][0].bytecnt = 8;
(*bd_table_ptr)[0][0][0].data0_1 = 0;
(*bd_table_ptr)[0][0][0].pid = 0;
(*bd_table_ptr)[0][0][0].own = 1;
// BDT for the second recieved packet
(*bd_table_ptr)[0][0][1].rsrvd1_0 = 0;
(*bd_table_ptr)[0][0][1].buffer_addr = (which)?USB1_RX_BUF2:USB0_RX_BUF2;
(*bd_table_ptr)[0][0][1].rsrvd15_8 = 0;
(*bd_table_ptr)[0][0][1].rsrvd31_26 = 0;
(*bd_table_ptr)[0][0][1].bytecnt = 8;
(*bd_table_ptr)[0][0][1].data0_1 = 1;
(*bd_table_ptr)[0][0][1].pid = 0;
(*bd_table_ptr)[0][0][1].own = 0;
// BDT for the first transmit packet
// echo packet XXXX
(*bd_table_ptr)[0][1][0].rsrvd1_0 = 0;
(*bd_table_ptr)[0][1][0].buffer_addr = (which)?USB1_RX_BUF1:USB0_RX_BUF1;
(*bd_table_ptr)[0][1][0].rsrvd15_8 = 0;
(*bd_table_ptr)[0][1][0].rsrvd31_26 = 0;
(*bd_table_ptr)[0][1][0].bytecnt = 8;
(*bd_table_ptr)[0][1][0].data0_1 = 0;
(*bd_table_ptr)[0][1][0].pid = 0;
(*bd_table_ptr)[0][1][0].own = 0;
// BDT for the second transmit packet
(*bd_table_ptr)[0][1][1].rsrvd1_0 = 0;
(*bd_table_ptr)[0][1][1].buffer_addr = (which)?USB1_RX_BUF2:USB0_RX_BUF2;
(*bd_table_ptr)[0][1][1].rsrvd15_8 = 0;
(*bd_table_ptr)[0][1][1].rsrvd31_26 = 0;
(*bd_table_ptr)[0][1][1].bytecnt = 8;
(*bd_table_ptr)[0][1][1].data0_1 = 1;
(*bd_table_ptr)[0][1][1].pid = 0;
(*bd_table_ptr)[0][1][1].own = 0;
usb_write_bdt_data(which, bd_table_ptr, 0);
return 0;
}
/* USB Test ipc interface*/
int linux_IO_Request(IpcPkt *req, IpcPkt *rsp);
int usb_test_req(int which, int req_code, int data1)
{
IpcPkt req, rsp;
memset(&req, 0, sizeof(IpcPkt));
memset(&rsp, 0, sizeof(IpcPkt));
req.length = htonl(sizeof(IpcPkt));
req.code = htonl(req_code);
req.data[0] = htonl(which);
req.data[1] = htonl(data1);
if (linux_IO_Request(&req, &rsp) < 0) {
_TRACE(DERROR, fprintf(LogFp, "USB turn on test request failed"));
return REQ_SOCKET_ERR;
}
if ((ntohl(rsp.code) & 0x1ff) != RSP_OK) {
_TRACE(DERROR, fprintf(LogFp, "BCP error when turn on USB test %x",
ntohl(rsp.code)));
return BCP_WRONG;
}
return 0;
}
/* For Bill's TERM and HOST CLTL */
int usb_test_req1(int req_code, int which, int func, int value)
{
IpcPkt req, rsp;
memset(&req, 0, sizeof(IpcPkt));
memset(&rsp, 0, sizeof(IpcPkt));
req.length = htonl(sizeof(IpcPkt));
req.code = htonl(req_code);
req.address = htonl(which);
req.size = htonl(func);
req.data[0] = htonl(value);
if (linux_IO_Request(&req, &rsp) < 0) {
_TRACE(DERROR, fprintf(LogFp, "USB turn on test request failed"));
return REQ_SOCKET_ERR;
}
if ((ntohl(rsp.code) & 0x1ff) != RSP_OK) {
_TRACE(DERROR, fprintf(LogFp, "BCP error when turn on USB test %x--",
ntohl(rsp.code)));
return BCP_WRONG;
}
return 0;
}
/* Get USB test stat */
/* Request code can be either BCP_LINESTATE or
BCP_USB_READ_STAT */
int usb_test_stat(int which, int code)
{
IpcPkt req, rsp;
memset(&req, 0, sizeof(IpcPkt));
memset(&rsp, 0, sizeof(IpcPkt));
req.length = htonl(sizeof(IpcPkt));
req.code = htonl(code);
req.data[0] = htonl(which);
if (linux_IO_Request(&req, &rsp) < 0) {
_TRACE(DERROR, fprintf(LogFp, "USB turn on test request failed"));
return REQ_SOCKET_ERR;
}
if ((ntohl(rsp.code) & 0x1ff) != RSP_DATA) {
_TRACE(DERROR, fprintf(LogFp, "BCP error when turn on USB test %x",
ntohl(rsp.code)));
return BCP_WRONG;
}
return ntohl(rsp.data[0]);
}
/* backdoor read packet data */
int usb_read_pkt_data(int which, int addr, int size)
{
IpcPkt req, rsp;
memset(&req, 0, sizeof(IpcPkt));
memset(&rsp, 0, sizeof(IpcPkt));
req.length = htonl(sizeof(IpcPkt));
req.code = htonl(BCP_USB_READ_DATA);
req.address = htonl(addr);
req.size = htonl(size);
req.data[0] = htonl(which);
if (linux_IO_Request(&req, &rsp) < 0) {
_TRACE(DERROR, fprintf(LogFp, "USB turn on test request failed"));
return REQ_SOCKET_ERR;
}
if ((ntohl(rsp.code) & 0x1ff) != RSP_DATA) {
_TRACE(DERROR, fprintf(LogFp, "BCP error when turn on USB test %x",
ntohl(rsp.code)));
return BCP_WRONG;
}
return ntohl(rsp.data[0]);
}
/* backdoor write packet data */
int usb_write_pkt_data(int which, int addr, int size, int data)
{
IpcPkt req, rsp;
memset(&req, 0, sizeof(IpcPkt));
memset(&rsp, 0, sizeof(IpcPkt));
req.length = htonl(sizeof(IpcPkt));
req.code = htonl(BCP_USB_WRITE_DATA);
req.address = htonl(addr);
req.size = htonl(size);
req.data[0] = htonl(which);
req.data[1] = htonl(data);
if (linux_IO_Request(&req, &rsp) < 0) {
_TRACE(DERROR, fprintf(LogFp, "USB turn on test request failed"));
return REQ_SOCKET_ERR;
}
if ((ntohl(rsp.code) & 0x1ff) != RSP_OK) {
_TRACE(DERROR, fprintf(LogFp, "BCP error when turn on USB test"));
return BCP_WRONG;
}
return 0;
}
/* wait for USB interrupt
return 0 if timeout
*/
int usb_wait_for_int(int which, int timeout, int interval)
{
int i, int_l, j, mi_int, usb_int;
if (which) usb_int = MI_INTR_USB1;
else usb_int = MI_INTR_USB0;
bcp_stall(1);
for (i=j=1; i<timeout; i+=interval, j++) {
int_l = CHECK_INTERRUPT;
if ((int_l & USB_MI_INT)!=0) {
mi_int = IO_READ(MI_EINTR_REG);
if ((mi_int & usb_int) !=0)
return 1;
else continue;
}
if (interval >= 0x1000) {
i -= interval - 1;
if (interval & 1) IO_READ(0x4980080);
else IO_WRITE(0x4980080, 0); /* backgroud noise */
} else bcp_stall(interval);
if (j % 2500 == 0 ) displayMsg("* Wait for interrupt *");
}
return 0;
}
int usb_set_bdt_ptr(BufferDescriptorTableType *bd_table_prt,
int usb_status,
BufferDescriptor **currBDT_ptr,
BufferDescriptor **sendBDT_ptr,
BufferDescriptor **nxtRcvBDT_ptr)
{
unsigned char *tmp = (unsigned char *) bd_table_prt;
BufferDescriptor *BDT_ptr = (BufferDescriptor *)tmp;
if (currBDT_ptr == NULL || sendBDT_ptr==NULL ||
nxtRcvBDT_ptr==NULL || bd_table_prt==NULL )
return -1;
*currBDT_ptr = &(BDT_ptr[usb_status>>2]);
*sendBDT_ptr = &(BDT_ptr[(usb_status>>2)^0x2]);
*nxtRcvBDT_ptr = &(BDT_ptr[(usb_status>>2)^0x3]);
return 0;
}
/* USB device test */
int usb_device_test(int which, int extra, int extra2, int extra3)
{
int otg_int;
int int_status, int_enable, usb_status, dir, i, int_mask;
BufferDescriptorTableType *bd_table_ptr;
BufferDescriptor *currBDT_ptr=NULL;
BufferDescriptor *sendBDT_ptr=NULL;
BufferDescriptor *nxtRcvBDT_ptr=NULL;
bd_table_ptr = (which)?(&usb1_bdt):(&usb0_bdt);
usb_device_setup(which);
usb_test_req(which, BCP_USB_TEST_ON, 0); // Turn on controller
set_usb_int_mask(which);
if (!which) IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_USB0);
else IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_USB1);
if (!usb_wait_for_int(which, 200000, 2)) {
_TRACE(DERROR, fprintf(LogFp, "\n\tError: wait for reset int to!"));
return -1;
}
if (usb_read_reg_test(which, USB0_INT_STAT_REG, 0x01, 0) == -1) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect for reset interrupt!"));
return -1;
}
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
usb_reg_write_test(which, USB0_ENDPT0_REG, 0x0D, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0xB9, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, 0x01, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
for (i=0; i<4; i++) {
if (!usb_wait_for_int(which, 20000, 1+0x1000*i)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect token dn <%d> or reset(0)", i));
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
usb_status = IO_READ(USB_REG_ADDR(which, USB0_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
dir = (usb_status >> 3) & 0x01;
int_mask = int_status & int_enable;
usb_read_bdt_data(which, bd_table_ptr, 0);
usb_set_bdt_ptr(bd_table_ptr, usb_status,
&currBDT_ptr, &sendBDT_ptr, &nxtRcvBDT_ptr);
if (int_mask == 0x08 && dir == 0) {
if (currBDT_ptr->pid == 0x0d) { /* SETUP */
sendBDT_ptr->bytecnt = currBDT_ptr->bytecnt;
sendBDT_ptr->pid = 0;
sendBDT_ptr->own = 1;
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
} else {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect setup token <%d>", i));
return -1;
}
} else {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect token done interrupt"));
return -1;
}
bcp_stall(1);
usb_write_bdt_data(which, bd_table_ptr, 0);
if (!usb_wait_for_int(which, 20000, 1)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect token dn tx <%d> ", i));
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
usb_status = IO_READ(USB_REG_ADDR(which, USB0_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
dir = (usb_status >> 3) & 0x01;
int_mask = int_status & int_enable;
usb_read_bdt_data(which, bd_table_ptr, 0);
usb_set_bdt_ptr(bd_table_ptr, usb_status,
&currBDT_ptr, &sendBDT_ptr, &nxtRcvBDT_ptr);
if (int_mask == 0x08 && dir == 1) {
nxtRcvBDT_ptr->bytecnt = 8;
nxtRcvBDT_ptr->pid = 0;
nxtRcvBDT_ptr->own = 1;
usb_write_bdt_data(which, bd_table_ptr, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
} else {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect token done interrupt for tx packet"));
return -1;
}
}
bcp_stall(1);
if (!usb_wait_for_int(which, 20000, 1)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect null data packet done "));
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
usb_status = IO_READ(USB_REG_ADDR(which, USB0_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
int_mask = int_status & int_enable;
dir = (usb_status >> 3) & 0x01;
if (int_mask == 0x08 && dir == 0) {
usb_read_bdt_data(which, bd_table_ptr, 0);
usb_set_bdt_ptr(bd_table_ptr, usb_status,
&currBDT_ptr, &sendBDT_ptr, &nxtRcvBDT_ptr);
if (currBDT_ptr->pid == 1 && currBDT_ptr->data0_1==0 &&
currBDT_ptr->bytecnt == 0) {
/* Try to recv SOF here */
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x04, 0);
BCP_STALL(100);
if (!usb_wait_for_int(which, 2000000, 100)) {
_TRACE(DERROR, fprintf(LogFp, "SOF token not received"));
return -1;
} else _TRACE(DLOG, fprintf(LogFp, "SOF token received!"));
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x00, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
} else {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect to receive NULL packet"));
return -1;
}
} else {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect token done before test done"));
return -1;
}
return 0;
}
/* USB line state test */
int usb_line_state_test(int which, int bcp, int extra2, int extra3)
{
int info, int_status, otg_int, state, stable, fixed=0;
int i, test[2]={0x40, 0x80};
if (bcp) return bcp_line_state_test(which, 0, 0, 0);
info = IO_READ(USB_REG_ADDR(which, USB0_ADD_INFO_REG));
if (!(info & 0x01)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: Host mode does not supported!"));
return -1;
}
usb_reg_write_test(which, USB0_CTL_REG, 0x08, 0);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x20, 0);
for (i=0; i<3; i++) {
if (!usb_wait_for_int(which, 320000, 10)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect line state change <%d>", i));
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
info = usb_test_stat(which, BCP_LINESTATE);
if (info == 0 && (!fixed)) {
_TRACE(DERROR, fprintf(LogFp, "Dual interrupts"));
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
/* Make sure the previous BVCI req is done */
state = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
fixed = 1;
i = -1;
continue;
}
if (info >=3 && (!fixed) && (i<1)) {
_TRACE(DERROR, fprintf(LogFp, "Lose SE0 interrupts"));
fixed = 1;
i=1;
}
if (((otg_int & 0x20) == 0) && (i<2)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: no line state interrupt <%d>", i));
return -1;
}
if ((i==2) && ((int_status & 0x20)==0)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: Expect Attach & Resume interrupt"));
return -1;
}
state = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
stable = IO_READ(USB_REG_ADDR(which, USB0_OTG_STATUS_REG));
if (i < 2) {
if ((state & test[i]) ==0) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: Wrong state <%d>", i));
return -1;
}
if ((stable & 0x20) == 0) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: line not stable <%d>", i));
return -1;
}
} else {
if ((state & 0x40) | (state & 0x80)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: Wrong state <%d>", i));
return -1;
}
/* Why? I guess it is a bug from arc */
stable = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
if (stable & 0x20) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: line is still stable <%d>", i));
return -1;
}
}
if (i==1) usb_reg_write_test(which, USB0_INT_ENB_REG, 0x20, 0);
if (i==2) {
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x00, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x00, 0);
}
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
/* Make sure the previous BVCI req is done */
state = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
}
return 0;
}
int usb_tb_setup(int which, int tb_is_device)
{
usb_test_req1(BCP_USB_HOST_CTL, which, 0, tb_is_device);
usb_test_req1(BCP_USB_TERM_CTL, which, 0, tb_is_device?0:1);
return 0;
}
int usb_otg_test(int which, int extra1, int extra2, int extra3)
{
int i, int_status, otg_int, otg_stat;
int check_pin[8] = {0x80, 0x80, 0x08, 0x08, 0x01, 0x01, 0x04, 0x04};
int wr_ctlp[7] = {0x00, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00} ;
int wr_ictl[8] = {0x00, 0x08, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00};
int mask[8] = {0x80, 0x80, 0x08, 0x08, 0x01, 0x01, 0x04, 0x04};
usb_tb_setup(which, 1); /* setup tb_is_device */
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x08, 0);
for (i=0; i<8; i++) {
_TRACE(DALL, fprintf(LogFp, "OTG test %d \n", i));
if (!usb_wait_for_int(which, 200000, 10)) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: expect otg int <%d>", i));
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
usb_test_req(which, BCP_USB_OTG_DONE, 0);
set_usb_int_mask(which);
/* * * Modified for Bill * * * /
{
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
displayMsg("Wait 100000 cycle");
bcp_stall(100000);
displayMsg("Turn off host enable");
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x00, 0);
bcp_stall(100000);
_TRACE(DERROR, fprintf(LogFp, "\n\tHost * * * "));
} */
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
usb_test_req(which, BCP_USB_OTG_DONE, 0);
BCP_STALL(100);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x00, 0);
break; // We only test usb_id interrupt here.
if ((otg_int & check_pin[i]) == 0) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: interrupt is unexpected <%d>", i));
return -1;
}
if (i<7) usb_reg_write_test(which, USB0_OTG_CTRL_REG, wr_ctlp[i], 0);
if (i&1) usb_reg_write_test(which, USB0_OTG_INT_EN_REG, wr_ictl[i], 0);
otg_stat = IO_READ(USB_REG_ADDR(which, USB0_OTG_STATUS_REG));
if (i & 1) {
if (otg_stat & mask[i]) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: otg status wrong <%d>", i));
return -1;
}
} else {
if ((otg_stat & mask[i]) == 0) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: otg status wrong <%d>", i));
return -1;
}
}
usb_reg_write_test(which, USB0_INT_STAT_REG, int_status, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
}
return 0;
}
int usb_host_pkt[16] = {0x00010203,0x04050607,0x08090a0b,0x0c0d0e0f,
0x10111213,0x14151617,0x18191a1b,0x1c1d1e1f,
0x20212223,0x24252627,0x28292a2b,0x2c2d2e2f,
0x30313233,0x34353637,0x38393a3b,0x3c3d3e3f};
int usb_host_setup(int which, int tx_addr, int rx_addr, int wdata)
{
BufferDescriptorTableType *bd_table_ptr;
int i, addr;
/* turn off host mode and clear SOF counter */
usb_reg_write_test(which, USB0_CTL_REG, 0x00, 0);
bd_table_ptr = (which)?(&usb1_bdt):(&usb0_bdt);
addr = tx_addr;
(*bd_table_ptr)[0][1][0].rsrvd1_0 = 0;
(*bd_table_ptr)[0][1][0].buffer_addr = addr;
(*bd_table_ptr)[0][1][0].rsrvd15_8 = 0;
(*bd_table_ptr)[0][1][0].rsrvd31_26 = 0;
(*bd_table_ptr)[0][1][0].bytecnt = 64;
(*bd_table_ptr)[0][1][0].data0_1 = 0;
(*bd_table_ptr)[0][1][0].pid = 0;
(*bd_table_ptr)[0][1][0].own = 1;
if (wdata)
for (i=0; i<64; i+=4)
IO_WRITE(addr+i, usb_host_pkt[i>>2]);
(*bd_table_ptr)[0][0][0].rsrvd1_0 = 0;
(*bd_table_ptr)[0][0][0].buffer_addr = rx_addr;
(*bd_table_ptr)[0][0][0].rsrvd15_8 = 0;
(*bd_table_ptr)[0][0][0].rsrvd31_26 = 0;
(*bd_table_ptr)[0][0][0].bytecnt = 64;
(*bd_table_ptr)[0][0][0].data0_1 = 0;
(*bd_table_ptr)[0][0][0].pid = 0;
(*bd_table_ptr)[0][0][0].own = 1;
usb_reg_write_test(which, USB0_ERR_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, 0x0e, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x0e, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x1b, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
usb_write_bdt_data(which, bd_table_ptr, 0);
/* delay 8us at least 4us */
if (wdata) {
long long st, end;
SIM_TIME(&st);
while (1==1) {
SIM_TIME(&end);
if ((end-st) > 8000) break;
BCP_STALL(10);
}
usb_reg_write_test(which, USB0_CTL_REG, 0x09, 0);
}
return 0;
}
/* USB simple host echo test */
int usb_simple_host_echo(int which, int a1, int a2, int a3)
{
int otg_int, int_status, int_enable, usb_status, dir, eq_dir, i,j;
int mask[3] = {0x04, 0x08, 0x08};
int exp_dir[3] = {0x00, 0x01, 0x00};
int pid[2] = {0x10, 0x90}, int_mask, rx, tx, tx_addr, rx_addr;
tx_addr = (which)?USB1_TX_BUF1:USB0_TX_BUF1;
rx_addr = (which)?USB1_RX_BUF1:USB0_RX_BUF1;
usb_host_setup(which, tx_addr, rx_addr, 1);
usb_tb_setup(which, 1);
for (i=0; i<3; i++) {
if (!usb_wait_for_int(which, 200000, 2)) {
if (i==0) { _TRACE(DERROR, fprintf(LogFp, "\n\tError: SOF TOK!")); }
else { _TRACE(DERROR, fprintf(LogFp, "\n\tError: Host echo %d!", i)); }
return -1;
}
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
usb_status = IO_READ(USB_REG_ADDR(which, USB0_STAT_REG));
int_mask = int_status & int_enable;
dir = (usb_status >> 3) & 0x01;
if (i) eq_dir = (dir == exp_dir[i])?1:0;
else eq_dir=1;
if ((int_mask == mask[i]) && (eq_dir)) {
if (i<2) usb_reg_write_test(which, USB0_TOKEN_REG, pid[i], 0);
else {
for (j=0; j<64; j+=4) {
tx = BD_IO_READ(tx_addr+j);
rx = BD_IO_READ(rx_addr+j);
if (tx != rx) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tError: simple echo failed %d %x %x!",
j, tx, rx));
//return -1;
}
}
usb_reg_write_test(which, USB0_CTL_REG, 0x00, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x00, 0);
}
} else {
_TRACE(DERROR, fprintf(LogFp, "\n\tError: expect TOK <%d>!",i));
return -1;
}
usb_reg_write_test(which, USB0_INT_STAT_REG, int_mask, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, otg_int, 0);
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
}
return 0;
}
int usb_bdt_sram(int which, int times, int extra0, int extra1)
{
int data[128], i, addr, ret, k;
int r_seed = time(NULL);
addr = (which)?USB1_BDT_ADDR:USB0_BDT_ADDR;
_TRACE(DLOG, fprintf(LogFp, "INFO: usb_bdt_test device=%d rand seed=%x",
which, r_seed));
srand(r_seed);
for (i=0; i<128; i++) {
data[i] = random()<<16;
data[i] |= (random() & 0xffff);
}
for (i=0; i<512; i+=4)
IO_WRITE(addr+i, data[i>>2]);
for (i=0; i<times; i++) {
k = random() % 128;
ret = IO_READ(addr + (k<<2));
_TRACE(DLOG, fprintf(LogFp, "\n\t INFO: BDT read @%x", addr+(k<<2)));
if (ret != data[k]) {
_TRACE(DERROR, fprintf(LogFp, "\n\tERROR: BDT %d rd=%x expect=%x",
which, ret, data[k]));
return -1;
}
}
for (i=0; i<times; i++) {
k = random() % 128;
data[0] = random()<<16;
data[0] |= (random() & 0xffff);
IO_WRITE(addr+(k<<2), data[0]);
ret = IO_READ(addr + (k<<2));
_TRACE(DLOG, fprintf(LogFp, "\n\t INFO: BDT wr @%x", addr+(k<<2)));
if (ret != data[0]) {
_TRACE(DERROR, fprintf(LogFp, "\n\tERROR: BDT %d wd=%x expect=%x",
which, ret, data[0]));
return -1;
}
}
for (i=0; i<512; i+=4)
IO_WRITE(addr+i, 0);
return 0;
}
int set_usb_int_mask(int which)
{
if (which == 0) IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_USB0);
else IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_SET_USB1);
return 0;
}
int clear_usb_int_mask(int which)
{
if (which == 0) IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_USB0);
else IO_WRITE(MI_INTR_EMASK_REG, MI_INTR_MASK_CLR_USB1);
return 0;
}
/* Wait for UI interrupt and also check if all tests are done
r_sram_addr and w_sram_addr was used to put more bkground
sram accessing noise.
return 1 if USB0 got interrupt and tests done
return 2 if USB1 got interrupt and tests done
return 3 if both got interrupt and all tests done
*/
int wait_for_ui_interrupt_test_on(int timeout, int interval,
int r_sram_addr, int w_sram_addr)
{
int start, cycles, int_l, stat, ret, usb_int, j, stat1;
for (start=0; start<timeout; start += interval) {
cycles = 0;
if (USB_SRAM0(r_sram_addr) || USB_SRAM1(r_sram_addr)) {
IO_READ(r_sram_addr);
cycles++;
}
if (USB_SRAM0(w_sram_addr) || USB_SRAM1(w_sram_addr)) {
IO_WRITE(w_sram_addr, 0);
cycles++;
}
if (interval > cycles) BCP_STALL(interval-cycles);
stat = usb_test_stat(0, BCP_USB_READ_STAT);
stat1 = usb_test_stat(1, BCP_USB_READ_STAT);
if ((stat | stat1) & BCP_USB_TEST_ERR) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tFailed: Test in Error stat %x %x", stat, stat1));
return -1;
}
int_l = CHECK_INTERRUPT;
if ((int_l & USB_MI_INT) != 0) {
ret = 0;
for (j=start; j<timeout; j+=interval) {
stat = usb_test_stat(0, BCP_USB_READ_STAT);
stat1 = usb_test_stat(1, BCP_USB_READ_STAT);
if (((BCP_USB_INT_MASK & stat) != 0) &&
((stat & 0xff)==0)){
usb_int = IO_READ(MI_EINTR_REG);
if (usb_int & MI_INTR_USB0) ret |= 1;
}
if (((BCP_USB_INT_MASK & stat1) != 0) &&
((stat1 & 0xff)==0)){
usb_int = IO_READ(MI_EINTR_REG);
if (usb_int & MI_INTR_USB1) ret |= 2;
}
if ((stat | stat1) & BCP_USB_TEST_ERR) {
_TRACE(DERROR, fprintf(LogFp,
"\n\tFailed: Test in Error stat %x %x",
stat, stat1));
ret = -1;
}
if (ret) break;
BCP_STALL(interval);
}
return ret;
}
}
return 0;
}
int is_memory_space(unsigned int addr)
{
if (addr >= 0x80000000) return 1;
if (addr <= 0x03000000) return 1;
return 0;
}
int bd_usb_read_data(int addr, char* sbuf, int size)
{
int num;
unsigned int data, cur_addr;
int len = size, i;
unsigned char *sCur = sbuf;
int shift[4] = {24, 16, 8, 0};
/* Error case only */
if (!is_memory_space(addr)) return 0;
cur_addr=addr&0xFFFFFFFC;
if (cur_addr != addr) {
data = BD_IO_READ(cur_addr);
num = addr &3;
for (i=num; i<4; i++)
*sCur++ = (unsigned char) (data >> shift[i]) & 0xff;
len -= (4-num);
cur_addr += 4;
}
num = (len & 0xFFFFFFFC);
for (i=0; i<num; i+=4) {
data = BD_IO_READ(cur_addr + i);
*sCur++ = (unsigned char) (data >> 24) & 0xff;
*sCur++ = (unsigned char) (data >> 16) & 0xff;
*sCur++ = (unsigned char) (data >> 8) & 0xff;
*sCur++ = (unsigned char) data & 0xff;
}
if ((len & 0x3) && (len>0)) {
data = BD_IO_READ(cur_addr + num);
for (i=0; i<(len&0x3); i++)
*sCur++ = (data >> shift[i]) & 0xff;
}
return 0;
}
int bd_usb_write_data(int addr, char* sbuf, int size)
{
int num;
unsigned int data, cur_addr;
int len = size, i;
unsigned char *sCur = sbuf;
int shift[4] = {24, 16, 8, 0};
unsigned int mask[4] = {0x00FFFFFF, 0xFF00FFFF, 0xFFFF00FF, 0xFFFFFF00};
/* Error case only */
if (!is_memory_space(addr)) return 0;
cur_addr = addr & 0xFFFFFFFC;
if (cur_addr != addr) {
data = BD_IO_READ(cur_addr);
num = addr &3;
for (i=num; i<4; i++) {
data &= mask[i];
data |= (*sCur++) << shift[i];
}
len -= (4-num);
BD_IO_WRITE(cur_addr, data);
cur_addr += 4;
}
data = BD_IO_READ(cur_addr);
num = (len & 0xFFFFFFFC);
for (i=0; i<num; i+=4) {
data = (*sCur++) << 24;
data |= (*sCur++) << 16;
data |= (*sCur++) << 8;
data |= (*sCur++);
BD_IO_WRITE(cur_addr+i, data);
}
if ((len & 0x3) && (len>0)){
data = BD_IO_READ(cur_addr + num);
for (i=0; i<(len&0x3); i++) {
data &= mask[i];
data |= (*sCur++) << shift[i];
}
BD_IO_WRITE(cur_addr+num, data);
}
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
BCP extented test suite
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * USB Setup Function * * * */
/* usb_core_host_setup(): configure USB core in host mode
usb_core_device_setup(): configure USB core in device mode
usb_ext_host_setup(): configure external module in host mode
usb_ext_device_setup(): configuer external module in device mode
*/
void wait_for_ns(int ns, int stall)
{
long long st, end;
int k=1;
SIM_TIME(&st);
while (1==1) {
SIM_TIME(&end);
if ((end-st) > ns) break;
BCP_STALL(stall);
if (((end-st) / 800000) >= k) {
k++;
displayMsg("Waiting in stall");
}
}
}
/* Function: configure USB core in host mode
Parameters
which --- which USB controller
enable_later --- enable USB in host mode later
*/
int usb_core_host_setup(int which, int enable_later)
{
/* turn off host mode and clear SOF counter */
usb_reg_write_test(which, USB0_CTL_REG, 0x00, 0);
usb_reg_write_test(which, USB0_ERR_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x0E, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x1b, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
usb_reg_write_test(which, USB0_ENDPT0_REG,
USB_ENDPT_HSHK | USB_ENDPT_TX_EN | USB_ENDPT_RX_EN,0);
usb_tb_setup(which, 1);
if (enable_later) {
wait_for_ns(8000, 10);
usb_reg_write_test(which, USB0_CTL_REG, 0x09, 0);
displayMsg("Setup Okay");
}
return 0;
}
/* Set USB external device as device
clear the error bits first */
int usb_ext_device_setup(int which)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x", test_on));
return -1;
}
/* Clear Error bit first */
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_TEST_ERR_SET);
/* Set ext device */
usb_test_req(which, BCP_USB_TEST_ON,
BCP_HOST_SETUP_ON | BCP_USB_TEST_ON_SET);
return 0;
}
/* set which core in device mode */
int usb_core_device_setup(int which, int int_rst_en)
{
int setup_reg[] = {
USB0_OTG_INT_EN_REG,
//USB0_OTG_CTRL_REG,
//USB0_CTL_REG,
USB0_INT_ENB_REG,
USB0_ERR_ENB_REG,
USB0_ADDR_REG,
USB0_FRM_NUML_REG,
USB0_FRM_NUMH_REG};
int i;
usb_reg_write_test(which, USB0_INT_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, 0xff, 0);
for (i=0; i<sizeof(setup_reg)/sizeof(int); i++)
usb_reg_write_test(which, setup_reg[i], 0, 0);
/* Make USB Core functional */
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x80, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x02, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
/* Enable interrupts */
usb_reg_write_test(which, USB0_INT_ENB_REG,
(int_rst_en)?0x0D:0x0C, 0);
usb_tb_setup(which, 0);
return 1;
}
/* Set USB external device as host
clear the error bits first */
int usb_ext_host_setup(int which)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x", test_on));
return -1;
}
/* Clear Error bit first */
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_TEST_ERR_SET);
/* Set ext device */
usb_test_req(which, BCP_USB_TEST_ON,
BCP_DEVICE_SETUP_ON | BCP_USB_TEST_ON_SET);
return 0;
}
/* USB external in device mode and try to rcv SOF */
int usb_ext_device_rcv_sof(int which)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x", test_on));
return -1;
}
/* Set ext device */
usb_test_req(which, BCP_USB_TEST_ON,
BCP_HOST_RCV_SOF_ON | BCP_USB_TEST_ON_SET);
return 0;
}
/* USB external in host mode and try to reset */
int usb_ext_host_reset(int which)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x", test_on));
return -1;
}
/* Set ext device */
usb_test_req(which, BCP_USB_TEST_ON,
BCP_DEVICE_RESET_ON | BCP_USB_TEST_ON_SET);
return 0;
}
/* Return endpoint control reg address */
int usb_endpt_addr(int which, int endpt)
{
int usb1_endpts[] ={ USB1_ENDPT0_REG, USB1_ENDPT1_REG,
USB1_ENDPT2_REG, USB1_ENDPT3_REG,
USB1_ENDPT4_REG, USB1_ENDPT5_REG,
USB1_ENDPT6_REG, USB1_ENDPT7_REG,
USB1_ENDPT8_REG, USB1_ENDPT9_REG,
USB1_ENDPT10_REG, USB1_ENDPT11_REG,
USB1_ENDPT12_REG, USB1_ENDPT13_REG,
USB1_ENDPT14_REG, USB1_ENDPT15_REG};
int usb0_endpts[] ={ USB0_ENDPT0_REG, USB0_ENDPT1_REG,
USB0_ENDPT2_REG, USB0_ENDPT3_REG,
USB0_ENDPT4_REG, USB0_ENDPT5_REG,
USB0_ENDPT6_REG, USB0_ENDPT7_REG,
USB0_ENDPT8_REG, USB0_ENDPT9_REG,
USB0_ENDPT10_REG, USB0_ENDPT11_REG,
USB0_ENDPT12_REG, USB0_ENDPT13_REG,
USB0_ENDPT14_REG, USB0_ENDPT15_REG};
return (which)?usb1_endpts[endpt]:usb0_endpts[endpt];
}
int usb_core_host_IN(
int which, /* which core */
int token_id, /* Which tocken pid(should be IN 0x90) */
int bdt_addr, /* BDT address(can be memory)
If it is memory, it should be 512 bytes aligned*/
void *bdt_ptr, /* BDT address for test */
int buf_addr, /* Memory buffer to receive the incoming data */
int pkt_len, /* Packet length */
int device_id, /* From which device */
int endpt, /* Send to which end point */
int odd, /* Odd/Even ? */
int ack_back, /* acknowledge */
int bdt_setup) /* Need to setup BDT reg */
{
BufferDescriptorTableType *bd_table_ptr;
int id, endpt_addr, endpt_ctrl, endpt_mask, endpt_set;
/* Make sure bdt_addr 512 bytes aligned */
if ((bdt_ptr == NULL) ||
((bdt_addr & 0x1ff)!=0)) {
_TRACE(DERROR, fprintf(LogFp, "Invalid BDT address 0x%x endpt=%d",
(unsigned int) bdt_addr, endpt));
return -1;
}
/* You only need to setup BDT table once you change it */
if (bdt_setup)
set_bdt_addr(which, bdt_addr);
/* Send device id */
id = IO_READ(which?USB1_ADDR_REG:USB0_ADDR_REG);
IO_WRITE(which?USB1_ADDR_REG:USB0_ADDR_REG,
(id & 0x80) | (device_id & 0x7f));
/* Setup BDT for IN transaction */
/* Host only use end point 0 */
bd_table_ptr = (BufferDescriptorTableType *) bdt_ptr;
(*bd_table_ptr)[0][0][odd].rsrvd1_0 = 0;
(*bd_table_ptr)[0][0][odd].buffer_addr = buf_addr;
(*bd_table_ptr)[0][0][odd].rsrvd15_8 = 0;
(*bd_table_ptr)[0][0][odd].rsrvd31_26 = 0;
(*bd_table_ptr)[0][0][odd].bytecnt = (pkt_len==0)?1024:pkt_len;
(*bd_table_ptr)[0][0][odd].data0_1 = 0; //(odd != 0)?1:0;
(*bd_table_ptr)[0][0][odd].pid = 0;
(*bd_table_ptr)[0][0][odd].own = 1;
endpt_addr = usb_endpt_addr(which, 0);
endpt_ctrl = IO_READ(endpt_addr);
endpt_set = ((ack_back)?USB_ENDPT_HSHK:0) | USB_ENDPT_RX_EN;
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_RX_EN;
if ((endpt_ctrl & endpt_mask) != endpt_set)
IO_WRITE(endpt_addr, (endpt_ctrl & (~endpt_mask)) | endpt_set);
usb_write_bdt_entry(bdt_addr, bd_table_ptr, 0, 0, odd);
usb_reg_write_test(which, USB0_TOKEN_REG, endpt | (token_id<<4), 0);
return 0;
}
int usb_core_host_OUT(
int which, /* which core */
int token_id, /* Which tocken pid(should be OUT or SETUP) */
int bdt_addr, /* BDT address(can be memory)
If it is memory, it should be 512 bytes aligned*/
void *bdt_ptr, /* BDT address for test */
int buf_addr, /* Memory buffer to receive the incoming data */
char *pkt_data, /* Packet data */
int pkt_len, /* Packet length */
int device_id,
int endpt, /* Send to which end point */
int odd, /* Odd/Even ? */
int ack_back, /* acknowledge */
int bdt_setup) /* Need to setup BDT reg */
{
BufferDescriptorTableType *bd_table_ptr;
int id, endpt_addr, endpt_ctrl, endpt_mask, endpt_set;
/* Make sure bdt_addr 512 bytes aligned */
if ((bdt_ptr == NULL) ||
((bdt_addr & 0x1ff)!=0)) {
_TRACE(DERROR, fprintf(LogFp, "Invalid BDT address 0x%x",
bdt_addr));
return -1;
}
/* You only need to setup BDT table once you change it */
if (bdt_setup)
set_bdt_addr(which, bdt_addr);
/* write data to memory */
bd_usb_write_data(buf_addr, pkt_data, pkt_len);
/* Send device id */
id = IO_READ(which?USB1_ADDR_REG:USB0_ADDR_REG);
IO_WRITE(which?USB1_ADDR_REG:USB0_ADDR_REG,
(id & 0x80) | (device_id & 0x7f));
/* Setup BDT for OUT transaction */
/* Host only use endpnt 0 */
bd_table_ptr = (BufferDescriptorTableType *) bdt_ptr;
(*bd_table_ptr)[0][1][odd].rsrvd1_0 = 0;
(*bd_table_ptr)[0][1][odd].buffer_addr = buf_addr;
//(*bd_table_ptr)[0][1][odd].buffer_addr = 0x04400000;
(*bd_table_ptr)[0][1][odd].rsrvd15_8 = 0;
(*bd_table_ptr)[0][1][odd].rsrvd31_26 = 0;
(*bd_table_ptr)[0][1][odd].bytecnt = pkt_len;
(*bd_table_ptr)[0][1][odd].data0_1 = 0; //(odd != 0)?1:0;
(*bd_table_ptr)[0][1][odd].pid = 0;
(*bd_table_ptr)[0][1][odd].own = 1;
endpt_addr = usb_endpt_addr(which, 0);
endpt_ctrl = IO_READ(endpt_addr);
endpt_set = (ack_back ? USB_ENDPT_HSHK : 0) | USB_ENDPT_TX_EN;
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_TX_EN;
if (token_id == BCP_TEST_SETUP_PID) {
endpt_set &= ~USB_ENDPT_DIS;
endpt_set |= (USB_ENDPT_TX_EN | USB_ENDPT_RX_EN);
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_RX_EN |
USB_ENDPT_RX_EN | USB_ENDPT_DIS;
}
if ((endpt_ctrl & endpt_mask) != endpt_set)
IO_WRITE(endpt_addr, (endpt_ctrl & (~endpt_mask)) | endpt_set);
usb_write_bdt_entry(bdt_addr, bd_table_ptr, 0, 1, odd);
usb_reg_write_test(which, USB0_TOKEN_REG, endpt | (token_id<<4), 0);
return 0;
}
int usb_core_device_IN(
int which, /* which USB core */
int token_id, /* Which tocken pid(should be IN 0x90) */
int bdt_addr, /* BDT address(can be memory) */
void *bdt_ptr, /* BDT address for test */
int buf_addr, /* Memory buffer to receive the incoming data */
char *pkt_data,
int pkt_len, /* Packet length */
int device_id,
int endpt, /* Send to which end point */
int odd, /* Odd/Even ? */
int ack_back, /* acknowledge */
int bdt_setup) /* Need to setup BDT reg */
{
BufferDescriptorTableType *bd_table_ptr;
int endpt_ctrl, endpt_set, endpt_mask, endpt_addr, id;
/* Make sure bdt_addr 512 bytes aligned */
if ((bdt_ptr == NULL) ||
(( bdt_addr & 0x1ff)!=0)) {
_TRACE(DERROR, fprintf(LogFp, "Invalid BDT address 0x%x",
bdt_addr));
return -1;
}
/* You only need to setup BDT table once you change it */
if (bdt_setup)
set_bdt_addr(which, bdt_addr);
/* Send device id */
id = IO_READ(which?USB1_ADDR_REG:USB0_ADDR_REG);
IO_WRITE(which?USB1_ADDR_REG:USB0_ADDR_REG,
(id & 0x80) | (device_id & 0x7f));
/* setup endpoint */
endpt_addr = usb_endpt_addr(which, endpt);
endpt_ctrl = IO_READ(endpt_addr);
endpt_set = (ack_back ? USB_ENDPT_HSHK : 0) | USB_ENDPT_TX_EN;
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_TX_EN;
if ((endpt_ctrl & endpt_mask) != endpt_set)
IO_WRITE(endpt_addr, (endpt_ctrl & (~endpt_mask)) | endpt_set);
bd_usb_write_data(buf_addr, pkt_data, pkt_len);
/* Setup BDT for IN transaction */
bd_table_ptr = (BufferDescriptorTableType *) bdt_ptr;
(*bd_table_ptr)[endpt][1][odd].rsrvd1_0 = 0;
(*bd_table_ptr)[endpt][1][odd].buffer_addr = buf_addr;
(*bd_table_ptr)[endpt][1][odd].rsrvd15_8 = 0;
(*bd_table_ptr)[endpt][1][odd].rsrvd31_26 = 0;
(*bd_table_ptr)[endpt][1][odd].bytecnt = pkt_len;
(*bd_table_ptr)[endpt][1][odd].data0_1 = 0; //(odd != 0)?1:0;
(*bd_table_ptr)[endpt][1][odd].pid = 0;
(*bd_table_ptr)[endpt][1][odd].own = 1;
usb_write_bdt_entry(bdt_addr, bd_table_ptr, endpt, 1, odd);
return 0;
}
int usb_core_device_OUT(
int which, /* which USB core */
int token_id, /* Which tocken pid(should be IN 0x90) */
int bdt_addr, /* BDT address(can be memory) */
void *bdt_ptr, /* BDT address for test */
int buf_addr, /* Memory buffer to receive the incoming data */
int pkt_len, /* Packet length */
int device_id,
int endpt, /* Send to which end point */
int odd, /* Odd/Even ? */
int ack_back, /* acknowledge */
int bdt_setup) /* Need to setup BDT reg */
{
BufferDescriptorTableType *bd_table_ptr;
int endpt_ctrl, endpt_set, endpt_mask, endpt_addr, id;
/* Make sure bdt_addr 512 bytes aligned */
if ((bdt_ptr == NULL) ||
((bdt_addr & 0x1ff)!=0)) {
_TRACE(DERROR, fprintf(LogFp, "Invalid BDT address 0x%x",
bdt_addr));
return -1;
}
/* You only need to setup BDT table once you change it */
if (bdt_setup)
set_bdt_addr(which, bdt_addr);
/* Send device id */
id = IO_READ(which?USB1_ADDR_REG:USB0_ADDR_REG);
IO_WRITE(which?USB1_ADDR_REG:USB0_ADDR_REG,
(id & 0x80) | (device_id & 0x7f));
/* setup endpoint */
endpt_addr = usb_endpt_addr(which, endpt);
endpt_ctrl = IO_READ(endpt_addr);
endpt_set = (ack_back ? USB_ENDPT_HSHK : 0) | USB_ENDPT_RX_EN;
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_RX_EN;
if (token_id == BCP_TEST_SETUP_PID) {
endpt_set &= ~USB_ENDPT_DIS;
endpt_set |= (USB_ENDPT_TX_EN | USB_ENDPT_RX_EN);
endpt_mask = USB_ENDPT_HSHK | USB_ENDPT_RX_EN |
USB_ENDPT_RX_EN | USB_ENDPT_DIS;
}
if ((endpt_ctrl & endpt_mask) != endpt_set)
IO_WRITE(endpt_addr, (endpt_ctrl & (~endpt_mask)) | endpt_set);
/* Setup BDT for OUT transaction */
bd_table_ptr = (BufferDescriptorTableType *) bdt_ptr;
(*bd_table_ptr)[endpt][0][odd].rsrvd1_0 = 0;
(*bd_table_ptr)[endpt][0][odd].buffer_addr = buf_addr;
(*bd_table_ptr)[endpt][0][odd].rsrvd15_8 = 0;
(*bd_table_ptr)[endpt][0][odd].rsrvd31_26 = 0;
(*bd_table_ptr)[endpt][0][odd].bytecnt = pkt_len;
(*bd_table_ptr)[endpt][0][odd].data0_1 = 0; //(odd != 0)?1:0;
(*bd_table_ptr)[endpt][0][odd].pid = 0;
(*bd_table_ptr)[endpt][0][odd].own = 1;
usb_write_bdt_entry(bdt_addr, bd_table_ptr, endpt, 0, odd);
return 0;
}
int usb_bd_write_test_pkt(int which, char *sbuf, int len)
{
int i, len4;
unsigned data;
unsigned char *stmp;
if (sbuf == NULL) return -1;
len4 = (len&0xfffc);
stmp = (unsigned char *) sbuf;
for (i=0; i<len4; i+=4) {
data = (*stmp++) << 24;
data |= ((*stmp++) << 16) & 0xff0000;
data |= ((*stmp++) << 8) & 0xff00;
data |= *stmp++;
usb_write_pkt_data(which, i, 4, data);
}
if (len > len4) {
data = 0;
for (i=len4; i<len; i++) {
data <<= 8;
data |= (*stmp++);
}
data <<= ((len4+4-len) << 3); /* x8 */
usb_write_pkt_data(which, len4, len-len4, data);
}
return 0;
}
int usb_bd_read_test_pkt(int which, char *sbuf, int len)
{
int i, len4;
unsigned char *stmp;
unsigned int data;
if (sbuf == NULL) return -1;
stmp = (char *) sbuf;
len4 = len & 0xfffc;
for (i=0; i<len4; i+=4) {
data = usb_read_pkt_data(which, i, 4);
*stmp++ = (data >> 24) & 0xff;
*stmp++ = (data >> 16) & 0xff;
*stmp++ = (data >> 8) & 0xff;
*stmp++ = data & 0xff;
}
if (len > len4) {
data = usb_read_pkt_data(which, len4, len-len4);
for (i=len4; i<len; i++) {
*stmp++ = (data >> 24) & 0xff;
data <<= 8;
}
}
return 0;
}
int usb_bd_setup_transaction_param(
int which,
int token_id,
int data_id,
int device_id,
char *pkt_data,
int pkt_len,
int endpt,
int ack_back)
{
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_TOKEN_PID|(token_id &0xff));
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_PKT_PID | BCP_USB_DATA0_PID);
//((data_id==0)?BCP_USB_DATA0_PID:BCP_USB_DATA1_PID));
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_ENDPT | (endpt & 0xf));
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_ACK | (ack_back & 0xf));
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_DEVICE_ID| (device_id & 0x7f));
usb_bd_write_test_pkt(which, pkt_data, pkt_len);
/* clear error bit */
usb_test_req(which, BCP_USB_TEST_ON, BCP_USB_TEST_ERR_SET);
/* set packet length */
if (pkt_len >= 1024) pkt_len=1023;
usb_test_req(which, BCP_USB_TEST_ON,
(BCP_USB_PKT_LEN_MASK & pkt_len) | BCP_USB_PKT_LEN_SET);
return 0;
}
/* Backdoor setup all necessary data
Then turn on the test */
int usb_ext_host_IN(
int which, /* which external host */
int token_id,
int data_id,
int device_id,
char *pkt_data,
int pkt_len,
int endpt,
int ack_back)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x",
test_on));
return -1;
}
usb_bd_setup_transaction_param(which, token_id, 0/* data_id*/, device_id,
pkt_data, pkt_len, endpt, ack_back);
usb_test_req(which, BCP_USB_TEST_ON,
BCP_DEVICE_IN_ON | BCP_USB_TEST_ON_SET);
return 0;
}
int usb_ext_host_OUT(
int which, /* which external host */
int token_id,
int data_id,
int device_id,
char *pkt_data,
int pkt_len,
int endpt,
int ack_back)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x",
test_on));
return -1;
}
usb_bd_setup_transaction_param(which, token_id, 0/* data_id*/, device_id,
pkt_data, pkt_len, endpt, ack_back);
usb_test_req(which, BCP_USB_TEST_ON,
BCP_DEVICE_OUT_ON | BCP_USB_TEST_ON_SET);
return 0;
}
int usb_ext_device_IN(
int which, /* which external host */
int token_id,
int data_id,
int device_id,
char *pkt_data,
int pkt_len,
int endpt,
int ack_back)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x",
test_on));
return -1;
}
usb_bd_setup_transaction_param(which, token_id, data_id, device_id,
pkt_data, pkt_len, endpt, ack_back);
usb_test_req(which, BCP_USB_TEST_ON,
BCP_HOST_IN_ON | BCP_USB_TEST_ON_SET);
return 0;
}
int usb_ext_device_OUT(
int which, /* which external host */
int token_id,
int data_id,
int device_id,
char *pkt_data,
int pkt_len,
int endpt,
int ack_back)
{
int test_on;
test_on = usb_test_stat(which, BCP_USB_READ_STAT);
if (test_on & BCP_USB_TEST_MASK) {
_TRACE(DERROR, fprintf(LogFp, "Other USB test is not done yet %x",
test_on));
return -1;
}
/* data_id should be control either DATA0_PID or DATA1_PID */
/* XXXXXXXXXXXXXXX */
usb_bd_setup_transaction_param(which, token_id, 0, /* data_id,*/ device_id,
pkt_data, pkt_len, endpt, ack_back);
usb_test_req(which, BCP_USB_TEST_ON,
BCP_HOST_OUT_ON | BCP_USB_TEST_ON_SET);
return 0;
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
BCP USB Extented tests
Build up transaction sequence first, then call
bcp_run_transactions()
to run all the transactions.
Note: Host/device mode have to be setup before you run the tests.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#define MAX_TRANSACTION 128
#define MAX_BUF_LEN 8192
BCP_USB_TRANS bcp_trans0[MAX_TRANSACTION], bcp_trans1[MAX_TRANSACTION];
int bcp_trans0_num, bcp_trans1_num;
/* Use to define the next entry of each endpt */
int bcp_bdt_odd0[MAX_ENDPNT][2];
int bcp_bdt_odd1[MAX_ENDPNT][2];
int usb_wait_for_test_done(int which, int timeout)
{
int test_stat1=0, test_stat2=0, t;
for (t = 0; t <timeout; t++) {
BCP_STALL(3);
if ((which & 0x1)==0)
test_stat1 = usb_test_stat(0, BCP_USB_READ_STAT);
if (which > 1)
test_stat2 =usb_test_stat(1, BCP_USB_READ_STAT);
if ((test_stat1 & BCP_USB_TEST_ERR) ||
(test_stat2 & BCP_USB_TEST_ERR)) {
_TRACE(DERROR, fprintf(LogFp, "STATUS WRONG 0x%x 0x%x",
test_stat1, test_stat2));
return -1;
}
if ( ((test_stat1 & BCP_USB_TEST_MASK)==0) &&
((test_stat2 & BCP_USB_TEST_MASK)==0))
break;
}
return 0;
}
int bcp_run_a_transaction(int which, int trans)
{
BCP_USB_TRANS *cur;
int nxt_snd, nxt_rcv, endpt;
/* In host mode, endpnt should be 0 */
if (which) {
cur = &bcp_trans1[trans];
endpt = (cur->host)?0:cur->endpt;
nxt_snd = bcp_bdt_odd1[endpt][1];
nxt_rcv = bcp_bdt_odd1[endpt][0];
} else {
cur = &bcp_trans0[trans];
endpt = (cur->host)?0:cur->endpt;
nxt_snd = bcp_bdt_odd0[endpt][1];
nxt_rcv = bcp_bdt_odd0[endpt][0];
}
if (cur->host) { /* USB core in Host mode */
if (cur->token_id == BCP_TEST_IN_PID) { /* IN */
usb_ext_device_IN(which, cur->token_id,
cur->data_id, cur->device_id, cur->pkt_data,
cur->pkt_len, cur->endpt, cur->ack_back);
usb_core_host_IN(which, cur->token_id,
cur->bdt_addr, cur->bdt_ptr, cur->buf_addr,
cur->pkt_len, cur->device_id, cur->endpt, nxt_rcv,
cur->ack_back, 0);
}
if ((cur->token_id == BCP_TEST_OUT_PID) ||
(cur->token_id == BCP_TEST_SETUP_PID)) { /* OUT */
usb_ext_device_OUT(which, cur->token_id,
cur->data_id, cur->device_id, cur->pkt_data,
cur->pkt_len, cur->endpt, cur->ack_back);
usb_core_host_OUT(which, cur->token_id,
cur->bdt_addr, cur->bdt_ptr, cur->buf_addr,
cur->pkt_data, cur->pkt_len, cur->device_id, cur->endpt,
nxt_snd, cur->ack_back, 0);
}
} else { /* USB core in device mode */
if (cur->token_id == BCP_TEST_IN_PID) { /* IN */
usb_core_device_IN(which, cur->token_id,
cur->bdt_addr, cur->bdt_ptr, cur->buf_addr,
cur->pkt_data, cur->pkt_len, cur->device_id, cur->endpt,
nxt_snd, cur->ack_back, 0);
usb_ext_host_IN(which, cur->token_id,
cur->data_id, cur->device_id, cur->pkt_data,
cur->pkt_len, cur->endpt, cur->ack_back);
}
if ((cur->token_id == BCP_TEST_OUT_PID) ||
(cur->token_id == BCP_TEST_SETUP_PID)) { /* OUT */
usb_core_device_OUT(which, cur->token_id,
cur->bdt_addr, cur->bdt_ptr, cur->buf_addr,
cur->pkt_len, cur->device_id, cur->endpt, nxt_rcv,
cur->ack_back, 0);
usb_ext_host_OUT(which, cur->token_id,
cur->data_id, cur->device_id, cur->pkt_data,
cur->pkt_len, cur->endpt, cur->ack_back);
}
}
return 0;
}
/* check transaction result
Return -1: Error found
1: Done */
int bcp_check_result(int which, int trans)
{
int int_status, int_enable, int_mask, ctl, status;
int ret = 0, i, endpt, tx, odd;
char s_ram[1024], s_pkt[1024];
BCP_USB_TRANS *cur;
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
int_enable = IO_READ(USB_REG_ADDR(which, USB0_INT_ENB_REG));
int_mask = int_status & int_enable;
if ((int_mask & 0x04) == 0x04) { /*SOF token */
/* Clear the SOF token */
usb_reg_write_test(which, USB0_INT_STAT_REG, 0x04, 0);
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
}
if ((int_mask & 0x08) == 0x08) { /* Token done */
cur = (which)?(&bcp_trans1[trans]):(&bcp_trans0[trans]);
status = IO_READ(USB_REG_ADDR(which, USB0_STAT_REG));
endpt = (status >> 4) & 0xf;
tx = (status >> 3) & 1;
odd = (status >> 2) & 1;
if (which) bcp_bdt_odd1[endpt][tx] = 1-odd;
else bcp_bdt_odd0[endpt][tx] = 1-odd;
if (cur->token_id == BCP_TEST_SETUP_PID) {
/* clear the txd_suspnd */
ctl = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
usb_reg_write_test(which, USB0_CTL_REG, ctl & (0xDF), 0);
}
ret = tx = 1;
bd_usb_read_data(cur->buf_addr, s_ram, cur->pkt_len);
usb_bd_read_test_pkt(which, s_pkt, cur->pkt_len);
for (i=0; i<cur->pkt_len; i++) {
if (s_ram[i] != s_pkt[i]) {
if ((cur->bdt_addr < 0x04900000) && (tx ==1)){
BCP_STALL(30);
bd_usb_read_data(cur->buf_addr, s_ram, cur->pkt_len);
i = tx = 0;
continue;
}
_TRACE(DERROR, fprintf(LogFp,
"\t\nFailed: usb data check wrong %d %2.2x %2.2x %2.2x",
i, s_ram[i], s_pkt[i], cur->pkt_data[i]));
ret = -1;
}
}
usb_reg_write_test(which, USB0_INT_STAT_REG, 0x08, 0);
int_status = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
} else {
_TRACE(DERROR, fprintf(LogFp,
"\t\nWARNING: wrong interrupt %x %x",int_status,int_enable));
ret = -3;
}
return ret;
}
/* return -1 if error happen */
int bcp_run_transactions(int back_noise)
{
int n1 = 0, n0 = 0; /* Test number */
int prev_done0 = 1, prev_done1 = 1, done, trans_done1=0, trans_done0=0;
while ((n0 < bcp_trans0_num) || (n1 < bcp_trans1_num)) {
if ((prev_done0) && (n0 < bcp_trans0_num)) {
/* Start new test on USB0 */
if (bcp_trans0[n0].host > 1) {
n0 = bcp_trans0_num;
} else bcp_run_a_transaction(0, n0);
}
/* Start a new test on USB1 */
if ((prev_done1) && (n1 < bcp_trans1_num)) {
if (bcp_trans1[n1].host > 1) {
n1 = bcp_trans1_num;
} else bcp_run_a_transaction(1, n1);
}
if (n0 >= bcp_trans0_num && n1 >= bcp_trans1_num)
break;
/* If test is done, disable interrupt */
if (n0 >= bcp_trans0_num && bcp_trans0_num>0) IO_WRITE(USB0_INT_ENB_REG, 0);
if (n1 >= bcp_trans1_num && bcp_trans1_num>0) IO_WRITE(USB1_INT_ENB_REG, 0);
if (back_noise) /* while tx/rx read BDT via CBus */
done = wait_for_ui_interrupt_test_on(200000, 1, 0x4980000, 0);
else
done = wait_for_ui_interrupt_test_on(200000, 1, 0, 0);
if (done & 1) {
prev_done0 = 1;
trans_done0 = bcp_check_result(0, n0);
if (trans_done0 > 0) n0 += trans_done0;
} else prev_done0 = 0;
if (done & 2) {
prev_done1 = 1;
trans_done1 = bcp_check_result(1, n1);
if (trans_done1 > 0) n1 += trans_done1;
} else prev_done1 = 0;
if (done == 0) {
_TRACE(DERROR, fprintf(LogFp, "Failed: Transaction timeout"));
return -1;
}
if (done == -1) return -1; /* Error happened */
if ((trans_done0 < 0) || (trans_done1<0)) {
if ((trans_done0==-1) || (trans_done1==-1)) {
_TRACE(DERROR, fprintf(LogFp, "Failed:Check result Wrong(2)"));
return -1;
} else
_TRACE(DERROR, fprintf(LogFp, "WARNING: Check result Wrong"));
}
}
IO_WRITE(USB0_INT_ENB_REG, 0xE);
IO_WRITE(USB1_INT_ENB_REG, 0xE);
return 0;
}
int wait_for_sof_done(int usb0, int usb1)
{
int int_status;
if (usb0) usb_ext_device_rcv_sof(0);
if (usb1) usb_ext_device_rcv_sof(1);
if (usb_wait_for_test_done(2, 20000) < 0)
return -1;
if (usb0) {
int_status = IO_READ(USB0_INT_STAT_REG);
if ((int_status & 0x04) == 0x04)
IO_WRITE(USB0_INT_STAT_REG, 0x04);
else return -2;
}
if (usb1) {
int_status = IO_READ(USB1_INT_STAT_REG);
if ((int_status & 0x04) == 0x04)
IO_WRITE(USB1_INT_STAT_REG, 0x04);
else return -3;
}
return 0;
}
int bcp_usb_host_setup(int usb0_host, int usb1_host)
{
int i, j;
/* Set for odd */
if (usb0_host <= 1) {
set_usb_int_mask(0);
for (i=0; i<MAX_ENDPNT; i++)
for (j=0; j<2; j++)
bcp_bdt_odd0[i][j] = 0;
}
if (usb1_host <= 1) {
set_usb_int_mask(1);
for (i=0; i<MAX_ENDPNT; i++)
for (j=0; j<2; j++)
bcp_bdt_odd1[i][j] = 0;
}
if (usb0_host == 1) {
usb_core_host_setup(0, 0);
usb_ext_device_setup(0);
} else {
if (usb0_host == 0) {
usb_ext_host_setup(0);
usb_core_device_setup(0, 0);
}
}
if (usb1_host == 1) {
usb_core_host_setup(1, 0);
usb_ext_device_setup(1);
} else {
if (usb1_host == 0) {
usb_ext_host_setup(1);
usb_core_device_setup(1, 0);
}
}
if ((usb0_host == 1) || (usb1_host == 1)) {
wait_for_ns(8000, 10);
if (usb0_host == 1) usb_reg_write_test(0, USB0_CTL_REG, 0x09, 0);
if (usb1_host == 1) usb_reg_write_test(1, USB0_CTL_REG, 0x09, 0);
}
if (usb_wait_for_test_done(2, 1000000) < 0) return -1;
return 0;
}
int bcp_get_spec_addr(int addr0[17], int addr1[17])
{
int mem_info;
int start1, addr_bits, i, cur_size;
int len[] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 13, 16, 32, 33, 64, 63,
128, 256};
mem_info = BCP_MEM_INFO;
addr_bits = mem_info & BCP_MEM_ADDR_MASK; /* in x64 mode */
start1 = 1<<(addr_bits-2);
if (start1 >= 0x00800000) start1 = 0x00800000;
addr0[0] = 0;
addr0[1] = 1;
addr1[0] = start1;
addr1[1] = start1 - 1;
for (i=2; i<15; i++) { /* 1k -- 4M boundary */
cur_size = 1 << (8+i);
if (cur_size < start1) {
addr0[i] = cur_size - (random() % len[i]);
addr1[i] = cur_size - (random() % len[i]) + start1;
} else {
addr0[i] = random() % 4000;
addr1[i] = random() % 4000 + start1;
}
}
addr0[15] = 0x01000000 | (random() % 4000);
addr1[15] = 0x80000000 | (random() % 4000);
addr1[16] = 0x01000000 | (random() % 4000);
addr0[16] = 0x80000000 | (random() % 4000);
if (addr_bits > 24) {
addr0[16] = 0x80030000 | (random() % 4000);
addr1[16] = 0x02000000 | (random() % 4000);
}
return 0;
}
/* Paramter:
usb?_host : 0 --- core in device mode
1 --- core in host mode
Others --- no test for this controller
*/
int hosts_test(int usb0_host, int usb1_host, int a3, int check)
{
int i, len1, len0, j, in_token, len;
char buf1[MAX_BUF_LEN], buf0[MAX_BUF_LEN];
int token[3] = { BCP_TEST_IN_PID, BCP_TEST_OUT_PID, BCP_TEST_SETUP_PID};
int spec_len[] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 13, 16, 32, 33, 64, 63,
128, 256};
int spec_addr0[17], spec_addr1[17];
bcp_get_spec_addr(spec_addr0, spec_addr1);
if ((usb0_host > 1) && (usb1_host > 1))
return 0; /* Both controll are inactive */
if (bcp_usb_host_setup(usb0_host, usb1_host) < 0) return -1;
/* (1) Send out data through endpoint 0-15(twice), pkt size=1
Purpose: let usb core access BDT table as much as possible*/
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 1"));
set_bdt_addr(0, USB0_BDT_SRAM);
set_bdt_addr(1, USB1_BDT_SRAM);
for (i=0; i<32; i++) {
bcp_trans0[i].host = usb0_host;
bcp_trans1[i].host = usb1_host;
bcp_trans0[i].token_id = bcp_trans1[i].token_id = BCP_TEST_OUT_PID;
bcp_trans0[i].data_id = bcp_trans1[i].data_id = (i>15)?1:0;
buf1[i] = rand() & 0xff;
buf0[i] = rand() & 0xff;
bcp_trans0[i].pkt_data = buf0 + i;
bcp_trans1[i].pkt_data = buf1 + i;
bcp_trans0[i].pkt_len = bcp_trans1[i].pkt_len = 1;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans0[i].ack_back = bcp_trans1[i].ack_back = 1;
bcp_trans0[i].bdt_addr = USB0_BDT_SRAM;
bcp_trans1[i].bdt_addr = USB1_BDT_SRAM;
bcp_trans0[i].bdt_ptr = &usb0_bdt;
bcp_trans1[i].bdt_ptr = &usb1_bdt;
bcp_trans0[i].endpt = bcp_trans1[i].endpt = i & 0xf;
}
if (usb0_host <= 1) bcp_trans0_num = 32;
if (usb1_host <= 1) bcp_trans1_num =32;
if (bcp_run_transactions(1)) return -1;
if (check || (usb0_host>1) || (usb1_host>1)) return 0;
/* (2) Same as (1), but BDT is in Memory */
set_bdt_addr(0, 0);
set_bdt_addr(1, 512);
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 2"));
for (i=0; i<16; i++) {
bcp_trans0[i].bdt_addr = 0;
bcp_trans1[i].bdt_addr = 512;
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
}
bcp_trans0_num = bcp_trans1_num =16;
if (bcp_run_transactions(1)) return -1;
/* (3) Same as (2) but now is Rcving */
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 3"));
for (i=0; i<16; i++) {
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans0[i].token_id = bcp_trans1[i].token_id = BCP_TEST_IN_PID;
}
if (bcp_run_transactions(1)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
/* (4) same as (3) but BDT in SRAM */
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 4"));
bcp_trans0_num = bcp_trans1_num =32;
set_bdt_addr(0, USB0_BDT_SRAM);
set_bdt_addr(1, USB1_BDT_SRAM);
for (i=0; i<32; i++) {
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans0[i].token_id = bcp_trans1[i].token_id = BCP_TEST_IN_PID;
bcp_trans0[i].bdt_addr = USB0_BDT_SRAM;
bcp_trans1[i].bdt_addr = USB1_BDT_SRAM;
}
if (bcp_run_transactions(1)) return -1;
/* (5) Same as (4) but random IN and OUT */
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 5"));
for (i=0; i<32; i++) {
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0x3ff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0x3ff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans0[i].token_id = token[rand() & 1];
bcp_trans1[i].token_id = token[rand() & 1];
bcp_trans0[i].endpt = rand() & 0xf;
bcp_trans1[i].endpt = rand() & 0xf;
}
if (bcp_run_transactions(1)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
/* (6) Some specific packet length test one IN another is out */
len0 = 0;
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 6"));
bcp_trans0_num = bcp_trans1_num = sizeof(spec_len)/sizeof(int);
for (j=0; j<MAX_BUF_LEN; j++) {
buf0[j] = rand() & 0xff;
buf1[j] = rand() & 0xff;
}
for (i=0; i<bcp_trans0_num; i++) {
bcp_trans0[i].host = usb0_host;
bcp_trans1[i].host = usb1_host;
in_token = rand() & 1;
bcp_trans0[i].token_id = token[in_token];
bcp_trans1[i].token_id = token[1-in_token];
bcp_trans0[i].pkt_data = buf0 + i;
bcp_trans1[i].pkt_data = buf1 + i;
bcp_trans0[i].pkt_len = bcp_trans1[i].pkt_len = spec_len[i];
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans0[i].buf_addr = spec_addr0[i];
bcp_trans1[i].buf_addr = spec_addr1[i];
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans0[i].ack_back = rand() & 0x1;
bcp_trans1[i].ack_back = rand() & 0x1;
bcp_trans0[i].bdt_addr = USB0_BDT_SRAM;
bcp_trans1[i].bdt_addr = USB1_BDT_SRAM;
bcp_trans0[i].bdt_ptr = &usb0_bdt;
bcp_trans1[i].bdt_ptr = &usb1_bdt;
bcp_trans0[i].endpt = rand() & 0xf;
bcp_trans1[i].endpt = rand() & 0xf;
}
if (bcp_run_transactions(0)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
/* Transfer packet 256/512 */
for (i=0; i<512; i++) {
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
}
for (i=0; i<2; i++) {
bcp_trans0[i].pkt_data = buf0 + (i+7);
bcp_trans1[i].pkt_data = buf1 + (i+10);
bcp_trans0[i].pkt_len = bcp_trans1[i].pkt_len = 1<<(8+i);
}
bcp_trans0_num = bcp_trans1_num = 2;
if (bcp_run_transactions(0)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
/* Transfer 1023 bytes */
bcp_trans0[i].pkt_data = buf0 + 6;
bcp_trans1[i].pkt_data = buf1 + 3;
bcp_trans0[i].pkt_len = bcp_trans1[i].pkt_len = 1023;
bcp_trans0_num = bcp_trans1_num = 1;
if (bcp_run_transactions(0)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
/* (7) Random check */
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 7"));
for (i=0; i<MAX_BUF_LEN; i++) {
buf0[i] = rand() & 0xff;
buf1[i] = rand() & 0xff;
}
for (j=0; j<4; j++) {
_TRACE(DLOG, fprintf(LogFp, "\n\nStart test 7.%d", j));
bcp_trans0_num = bcp_trans1_num = 0;
len0 = len1 = 0;
for (i=0; i<8; i++) {
bcp_trans0[i].host = usb0_host;
bcp_trans1[i].host = usb1_host;
bcp_trans0[i].token_id = token[random() % 3];
bcp_trans1[i].token_id = token[random() % 3];
if (i==0) len = rand() % 1023;
else len = rand() % 128;
if (len0 < 1024) {
if ((len+len0) >=1024) len = 1024 - len0;
len0 += len;
bcp_trans0[i].pkt_data = buf0 + len;
bcp_trans0[i].pkt_len = len;
bcp_trans0[i].buf_addr = 0x1000 + (rand() & 0xfff);
bcp_trans0[i].device_id = rand() & 0x7f;
bcp_trans0[i].ack_back = rand() & 1;
if (bcp_trans0[i].token_id == BCP_TEST_SETUP_PID)
bcp_trans0[i].ack_back = 1;
bcp_trans0[i].bdt_addr = USB0_BDT_SRAM;
bcp_trans0[i].bdt_ptr = &usb0_bdt;
bcp_trans0[i].endpt = rand() & 0xf;
bcp_trans0_num++;
}
if (i==0) len = rand() % 1023;
else len = rand() % 128;
if (len1 < 1024) {
if ((len+len1) >=1024) len = 1024 - len1;
len1 += len;
bcp_trans1[i].pkt_data = buf1 + len;
bcp_trans1[i].pkt_len = len;
bcp_trans1[i].buf_addr = 0x3000 + (rand() & 0xfff);
bcp_trans1[i].device_id = rand() & 0x7f;
bcp_trans1[i].ack_back = rand() & 1;
if (bcp_trans1[i].token_id == BCP_TEST_SETUP_PID)
bcp_trans1[i].ack_back = 1;
bcp_trans1[i].bdt_addr = USB1_BDT_SRAM;
bcp_trans1[i].bdt_ptr = &usb1_bdt;
bcp_trans1[i].endpt = rand() & 0xf;
bcp_trans1_num++;
}
if (len0 >= 1024 && len1 >= 1024)
break;
}
if (bcp_run_transactions(0)) return -1;
if (wait_for_sof_done(usb0_host==1, usb1_host==1) != 0)
return -1;
}
return 0;
}
/* Paramter:
usb?_host : 0 --- core in device mode
1 --- core in host mode
*/
int hosts_one_test(int usb0_host, int usb1_host, int bdt_addr, int buf_addr)
{
char buf1[MAX_BUF_LEN], buf0[MAX_BUF_LEN];
if ((usb0_host > 1) && (usb1_host > 1))
return 0; /* Both controll are inactive */
if (bcp_usb_host_setup(usb0_host, usb1_host) < 0) return -1;
set_bdt_addr(0, bdt_addr);
set_bdt_addr(1, bdt_addr);
bcp_trans0[0].host = usb0_host;
bcp_trans1[0].host = usb1_host;
bcp_trans0[0].token_id = bcp_trans1[0].token_id = BCP_TEST_OUT_PID;
bcp_trans0[0].data_id = bcp_trans1[0].data_id = 0;
bcp_trans0[0].pkt_data = buf0;
bcp_trans1[0].pkt_data = buf1;
bcp_trans0[0].pkt_len = bcp_trans1[0].pkt_len = 1;
bcp_trans0[0].buf_addr = buf_addr;
bcp_trans1[0].buf_addr = buf_addr;
bcp_trans0[0].device_id = 106;
bcp_trans1[0].device_id = 106;
bcp_trans0[0].ack_back = bcp_trans1[0].ack_back = 1;
bcp_trans0[0].bdt_addr = bdt_addr;
bcp_trans1[0].bdt_addr = bdt_addr;
bcp_trans0[0].bdt_ptr = &usb0_bdt;
bcp_trans1[0].bdt_ptr = &usb1_bdt;
bcp_trans0[0].endpt = bcp_trans1[0].endpt = 11;
if (usb0_host <=1 ) bcp_trans0_num = 1;
else bcp_trans0_num = 0;
if (usb1_host <=1 ) bcp_trans1_num = 1;
else bcp_trans1_num = 0;
if (bcp_run_transactions(0)) return -1;
return 0;
}
/* ############## USB for random tests ################ */
/** 100 --- store USB0 rx odd data
104 --- store USB0 tx odd data
110 --- store USB1 rx odd data
114 --- store USB1 tx odd data
**/
#define USB0_MAGIC_ADDR 0x100
#define USB1_MAGIC_ADDR 0x110
int set_usb_odd(int which, int tx, int bit, int odd)
{
int addr, data;
unsigned int mask[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000};
if (which) addr = USB1_MAGIC_ADDR + (tx?4:0);
else addr = USB0_MAGIC_ADDR + (tx?4:0);
if (bit >= 16) bit = 0;
data = BD_IO_READ(addr);
if (odd) data |= mask[bit];
else data &= ~mask[bit];
BD_IO_WRITE(addr, data);
return 0;
}
int get_usb_odd(int which, int tx, int bit)
{
int addr, data;
unsigned int mask[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
0x100, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000, 0x8000};
if (which) addr = USB1_MAGIC_ADDR + (tx?4:0);
else addr = USB0_MAGIC_ADDR + (tx?4:0);
if (bit >= 16) bit = 0;
data = BD_IO_READ(addr);
if (data & mask[bit]) return 1;
return 0;
}
int hosts_setup(int usb0_host, int usb1_host, int bdt_addr0, int bdt_addr1)
{
if ((usb0_host > 1) && (usb1_host > 1))
return 0; /* Both controll are inactive */
if (bcp_usb_host_setup(usb0_host, usb1_host) < 0) return -1;
BD_IO_WRITE(USB0_MAGIC_ADDR, 0);
BD_IO_WRITE(USB0_MAGIC_ADDR+4, 0);
BD_IO_WRITE(USB1_MAGIC_ADDR, 0);
BD_IO_WRITE(USB1_MAGIC_ADDR+4, 0);
set_bdt_addr(0, bdt_addr0);
set_bdt_addr(1, bdt_addr1);
return 0;
}
int usb_random_transaction(int which, int pkt_addr)
{
int endpt, i;
char buf[MAX_BUF_LEN];
int token[3] = { BCP_TEST_IN_PID, BCP_TEST_OUT_PID, BCP_TEST_SETUP_PID};
for (i=0; i<MAX_BUF_LEN; i++)
buf[i] = rand() & 0xff;
bcp_trans0[0].host = 0;
bcp_trans1[0].host = 0;
bcp_trans0[0].token_id =
bcp_trans1[0].token_id = token[rand() % 2];
bcp_trans0[0].data_id = bcp_trans1[0].data_id = 0;
bcp_trans0[0].pkt_data = bcp_trans1[0].pkt_data = buf;
/* Make 0-63 bytes more often */
if (rand() % 8 == 1)
bcp_trans0[0].pkt_len = bcp_trans1[0].pkt_len = rand()%1024;
else bcp_trans0[0].pkt_len = bcp_trans1[0].pkt_len = rand()%64;
bcp_trans0[0].buf_addr = bcp_trans1[0].buf_addr = pkt_addr;
bcp_trans0[0].device_id = bcp_trans1[0].device_id = rand() % 128;
bcp_trans0[0].ack_back = bcp_trans1[0].ack_back = rand() & 1;
bcp_trans0[0].bdt_addr = USB0_BDT_ADDR;
bcp_trans1[0].bdt_addr = USB1_BDT_ADDR;
bcp_trans0[0].bdt_ptr = &usb0_bdt;
bcp_trans1[0].bdt_ptr = &usb1_bdt;
endpt = rand() & 0xf;
bcp_trans0[0].endpt = bcp_trans1[0].endpt = endpt;
/* IN token send OUT rx for device */
if (which) {
if (bcp_trans1[0].token_id != BCP_TEST_IN_PID) {
bcp_bdt_odd1[endpt][0] = get_usb_odd(which, 0, endpt);
set_usb_odd(which, 0, endpt, 1-bcp_bdt_odd1[endpt][0]);
} else {
bcp_bdt_odd1[endpt][1] = get_usb_odd(which, 1, endpt);
set_usb_odd(which, 1, endpt, 1-bcp_bdt_odd1[endpt][1]);
}
} else {
if (bcp_trans0[0].token_id != BCP_TEST_IN_PID) {
bcp_bdt_odd0[endpt][0] = get_usb_odd(which, 0, endpt);
set_usb_odd(which, 0, endpt, 1-bcp_bdt_odd0[endpt][0]);
} else {
bcp_bdt_odd0[endpt][1] = get_usb_odd(which, 1, endpt);
set_usb_odd(which, 1, endpt, 1-bcp_bdt_odd0[endpt][1]);
}
}
bcp_run_a_transaction(which, 0);
if (!usb_wait_for_int(which, 200000, 2)) {
_TRACE(DERROR, fprintf(LogFp, "\n\tError: wait for interrupt failed!"));
return -1;
}
if (bcp_check_result(which, 0) != 1) {
_TRACE(DERROR, fprintf(LogFp, "\n\tError: Result check error!"));
return -1;
}
return 0;
}
int bcp_check_otg_int_stat(char *prefix, int which,
int wait_int, int int_mask, int int_res,
int stat_mask, int stat_res)
{
int otg_int, otg_status;
if (wait_int) {
if (!usb_wait_for_int(which, 400000, 10)) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s missing int", prefix));
return -1;
}
}
if (int_mask) {
otg_int = IO_READ(USB_REG_ADDR(which, USB0_OTG_INT_STAT_REG));
if ((otg_int & int_mask) != int_res) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s intr wrong <%x>",
prefix, otg_int));
return -1;
}
}
if (stat_mask) {
otg_status = IO_READ(USB_REG_ADDR(which, USB0_OTG_STATUS_REG));
if ((otg_status & stat_mask) != stat_res) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s otg status <%x>",
prefix, otg_status));
return -1;
}
}
return 0;
}
int bcp_clear_int(int which)
{
usb_reg_write_test(which, USB0_OTG_INT_STAT_REG, 0xff, 0);
usb_reg_write_test(which, USB0_INT_STAT_REG, 0xff, 0);
return 0;
}
int bcp_check_ls_int(char *prefix, int which, int ls, int int_mask, int int_res)
{
int ctl_stat, int_stat;
if (ls == 1) { /* Low speed check */
ctl_stat = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
if ((ctl_stat & 0xC0) != 0) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s should in low speed <%x>",
prefix, ctl_stat));
return -1;
}
}
if (ls == 2) { /* High speed check */
ctl_stat = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
if ((ctl_stat & 0xC0) == 0) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s should not low sp <%x>",
prefix, ctl_stat));
return -1;
}
}
if (int_mask) {
int_stat = IO_READ(USB_REG_ADDR(which, USB0_INT_STAT_REG));
if ((int_stat & int_mask) != int_res) {
_TRACE(DERROR, fprintf(LogFp, "\n\t %s int should set <%x>",
prefix, int_stat));
return -1;
}
}
return 0;
}
int bcp_line_state_test(int which, int extra1, int extra2, int extra3)
{
int clt_stat;
#if 0
/* * * * * Test Host mode * * * * * */
if (which) bcp_usb_host_setup(2, 1);
else bcp_usb_host_setup(1, 2);
displayMsg("BB disconnect and in host mode ");
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x09, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x0C, 0);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0, 0);
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(0)", which, 0, 0, 0, 0x81, 0x81) == -1)
return -1;
displayMsg("Connect mini-A cable...");
bcp_clear_int(which);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(1)", which, 1, 0x80, 0x80, 0x80,0) == -1)
return -1;
displayMsg("disconnect mini-A cable...");
wait_for_ns(20000, 100);
bcp_clear_int(which);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
if (bcp_check_otg_int_stat("(2)",which,1,0x80, 0x80, 0x80,0x80) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0, 0);
displayMsg("Attached a low speed device w mini-A cable");
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x40, 0);
bcp_clear_int(which);
wait_for_ns(2000, 100);
usb_test_req1(BCP_USB_HOST_CTL, which, 1, 1);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(3)", which, 1, 0, 0, 0,0) == -1)
return -1;
if (bcp_check_ls_int("(3.0)", which, 1, 0x40, 0x40) == -1)
return -1;
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(3.1)",which,0, 0x80, 0x80, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
wait_for_ns(400000, 1000);
/* Should get RST interrupt */
/* Bill might forget to set d- */
displayMsg("Disconnect the low speed device ...");
bcp_clear_int(which);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x41, 0);
if (bcp_check_otg_int_stat("(4)", which, 0, 0, 0, 0, 0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0xC, 0);
/* Connect low-speed device */
/* Should get Attach int, but because the previous problem */
displayMsg("Connect the low speed device again ...");
bcp_clear_int(which);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(5)", which, 0, 0, 0, 0, 0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
wait_for_ns(400000, 1000);
displayMsg("Disconnect the mini-A cable w device");
wait_for_ns(2000000, 1000);
bcp_clear_int(which);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
if (bcp_check_otg_int_stat("(6)", which, 1, 0x80, 0x80,0x81,0x81)==-1)
return -1;
displayMsg("Connect full-speed device with cable ");
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x40, 0);
wait_for_ns(2000000, 1000);
bcp_clear_int(which);
wait_for_ns(2000, 100);
usb_test_req1(BCP_USB_HOST_CTL, which, 1, 0);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(7)", which, 1, 0, 0, 0,0) == -1)
return -1;
if (bcp_check_ls_int("(7.0)", which, 2, 0x40, 0x40) == -1)
return -1;
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(7.1)",which,0, 0x80, 0x80, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
wait_for_ns(400000, 1000);
/* ASk Bill */
displayMsg("Disconnect full speed device ...");
bcp_clear_int(which);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x1, 0);
if (bcp_check_otg_int_stat("(8)", which, 0, 0, 0, 0, 0) == -1)
//if (bcp_check_otg_int_stat("(8)", which, 1, 0, 0, 0, 0) == -1)
return -1;
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(8.1)",which,0, 0, 0, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0xC, 0);
displayMsg("Connect full speed device again");
bcp_clear_int(which);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
usb_reg_write_test(which, USB0_INT_ENB_REG, 0x40, 0);
if (bcp_check_otg_int_stat("(9)", which, 1, 0, 0, 0,0) == -1)
return -1;
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(9.1)",which,0, 0, 0, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x3C, 0);
wait_for_ns(400000, 1000);
/* Ask Bill */
displayMsg("disconnect device and cable");
wait_for_ns(2000000, 1000);
bcp_clear_int(which);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 1, 0);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
if (bcp_check_otg_int_stat("(10)", which, 1, 0x80, 0x80,0x81,0x81)==-1)
return -1;
//if (bcp_check_ls_int("(10.0)", which, 0, 0x01, 0x01) == -1)
if (bcp_check_ls_int("(10.0)", which, 0, 0, 0x01) == -1)
return -1;
#endif
/* * * * * * * Test for device mode * * * * * * * * */
usb_ext_host_setup(which);
usb_core_device_setup(which, 0);
set_usb_int_mask(which);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0 , 0);
usb_reg_write_test(which, USB0_CTL_REG, 0, 0);
if (usb_wait_for_test_done(which?2:1, 20000) < 0)
return -1;
#if 0
displayMsg("BB in device mode and disconnected");
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
wait_for_ns(3000000, 1000);
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x04, 0);
displayMsg("BB(device) connect Mini-A cable ...");
wait_for_ns(1000000, 1000);
bcp_clear_int(which);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(A)", which, 1, 0x80, 0x80, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x0C, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x09, 0);
wait_for_ns(1000000, 1000);
clt_stat = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
if ((clt_stat & 0xC0) != 0x40) {
_TRACE(DERROR, fprintf(LogFp, "\n\t BB should be se0 %x", clt_stat));
return -1;
}
displayMsg("disconnect mini-A cable...");
usb_reg_write_test(which, USB0_INT_ENB_REG, 0, 0);
wait_for_ns(20000, 1000);
bcp_clear_int(which);
wait_for_ns(20000, 1000);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
if (bcp_check_otg_int_stat("(B)",which,1,0x80, 0x80, 0x80,0x80) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x04, 0);
displayMsg("BB in disconnected device mode again...");
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
wait_for_ns(3000000, 1000);
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x04, 0);
displayMsg("BB(device) connect host with mini-A cable");
wait_for_ns(1000000, 1000);
bcp_clear_int(which);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0x80, 0);
usb_tb_setup(which, 1);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
if (bcp_check_otg_int_stat("(C)", which, 1, 0x80, 0x80, 0x80,0) == -1)
return -1;
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x0C, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x09, 0);
wait_for_ns(1000000, 1000);
clt_stat = IO_READ(USB_REG_ADDR(which, USB0_CTL_REG));
if ((clt_stat & 0xC0) != 0x80) {
_TRACE(DERROR, fprintf(LogFp, "\n\t BB be J %x", clt_stat));
return -1;
}
#endif
displayMsg("BB in disconnected mode again(2) ...");
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_CTL_REG, 0x01, 0);
usb_reg_write_test(which, USB0_OTG_CTRL_REG, 0x04, 0);
wait_for_ns(3000000, 1000);
displayMsg("Connect to Mini-B cable");
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
wait_for_ns(2000000, 1000);
if (bcp_check_otg_int_stat("(D)", which, 0, 0x80, 0, 0x80,0x80) == -1)
return -1;
displayMsg("Connect a device ...");
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
wait_for_ns(2000000, 1000);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0xA1, 0);
if (bcp_check_otg_int_stat("(E)", which, 1, 0x80, 0, 0x81,0x81) == -1)
return -1;
displayMsg("disconnect a device ...");
wait_for_ns(200000, 1000);
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 1, 0);
if (bcp_check_otg_int_stat("(F)", which, 1, 0x80, 0x0, 0x80,0x80) == -1)
return -1;
displayMsg("Connect a device again ...");
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
wait_for_ns(2000000, 1000);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0xA1, 0);
if (bcp_check_otg_int_stat("(G)", which, 1, 0x80, 0, 0x81,0x81) == -1)
return -1;
displayMsg("disconnect a device with cable ...");
wait_for_ns(200000, 1000);
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 0);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 0);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0, 0);
usb_reg_write_test(which, USB0_INT_ENB_REG, 1, 0);
if (bcp_check_otg_int_stat("(H)", which, 1, 0x80, 0x0, 0x80,0x80) == -1)
return -1;
displayMsg("Connect a device with cable ...");
bcp_clear_int(which);
usb_tb_setup(which, 0);
usb_test_req1(BCP_USB_TERM_CTL, which, 1, 1);
usb_test_req1(BCP_USB_BIAS_CTL, which, 1, 1);
wait_for_ns(2000000, 1000);
usb_reg_write_test(which, USB0_OTG_INT_EN_REG, 0xAD, 0);
if (bcp_check_otg_int_stat("(H)", which, 1, 0x81, 0x1, 0x81,0x81) == -1)
return -1;
displayMsg("BCP linestate test done");
wait_for_ns(1000000, 1000);
return 0;
}