summaryrefslogtreecommitdiffstats
path: root/arch/arm/cpu/arm926ejs/aspeed/PHY.c
blob: db73a70b4d1124056eade2752c46d3405f4d72d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
/*
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#define PHY_C
static const char ThisFile[] = "PHY.c";

#include "SWFUNC.H"

#ifdef SLT_UBOOT
  #include <common.h>
  #include <command.h>
  #include "COMMINF.H"
  #include "STDUBOOT.H"
#endif
#ifdef SLT_DOS
  #include <stdio.h>
  #include <stdlib.h>
  #include <conio.h>
  #include <string.h>
  #include "COMMINF.H"
#endif
  
#include "PHY.H"                    
#include "TYPEDEF.H"
#include "IO.H"

ULONG	PHY_09h;
ULONG	PHY_18h;
ULONG	PHY_1fh;
ULONG	PHY_06hA[7];
ULONG	PHY_11h;
ULONG	PHY_12h;
ULONG	PHY_15h;
ULONG	PHY_06h;
char	PHYID[256];
ULONG	PHY_00h;

//------------------------------------------------------------
// PHY R/W basic
//------------------------------------------------------------
void phy_write (int adr, ULONG data) {
    int timeout = 0;
    
	if (AST2300_NewMDIO) {
		WriteSOC_DD( MAC_PHYBASE + 0x60, ( data << 16 ) | MAC_PHYWr_New | (PHY_ADR<<5) | (adr & 0x1f));

		while ( ReadSOC_DD( MAC_PHYBASE + 0x60 ) & MAC_PHYBusy_New ) {
			if ( ++timeout > TIME_OUT_PHY_RW ) {
				if (!BurstEnable) 
#ifdef SLT_DOS
                    fprintf(fp_log, "[PHY-Write] Time out: %08lx\n", ReadSOC_DD( MAC_PHYBASE + 0x60 ) );
#endif
                    FindErr( Err_PHY_TimeOut );
				break;
			}
		}
	} 
	else {
		WriteSOC_DD( MAC_PHYBASE + 0x64, data );

		WriteSOC_DD( MAC_PHYBASE + 0x60, MDC_Thres | MAC_PHYWr | (PHY_ADR<<16) | ((adr & 0x1f) << 21));

		while ( ReadSOC_DD( MAC_PHYBASE + 0x60 ) & MAC_PHYWr ) {
			if ( ++timeout > TIME_OUT_PHY_RW ) {
#ifdef SLT_DOS
                if (!BurstEnable) 
				    fprintf(fp_log, "[PHY-Write] Time out: %08lx\n", ReadSOC_DD( MAC_PHYBASE + 0x60 ) );
#endif
                FindErr( Err_PHY_TimeOut );
				break;
			}
		}
	} // End if (AST2300_NewMDIO)

	if ( DbgPrn_PHYRW ) 
	    printf ("[Wr ]%02d: %04lx\n", adr, data);
} // End void phy_write (int adr, ULONG data)

//------------------------------------------------------------
ULONG phy_read (int adr) {
    int timeout = 0;
    
	if ( AST2300_NewMDIO ) {
		WriteSOC_DD( MAC_PHYBASE + 0x60, MAC_PHYRd_New | (PHY_ADR << 5) | ( adr & 0x1f ) );

		while ( ReadSOC_DD( MAC_PHYBASE + 0x60 ) & MAC_PHYBusy_New ) {
			if ( ++timeout > TIME_OUT_PHY_RW ) {
				if ( !BurstEnable ) 
#ifdef SLT_DOS
                    fprintf(fp_log, "[PHY-Read] Time out: %08lx\n", ReadSOC_DD( MAC_PHYBASE + 0x60 ));
#endif
                    FindErr( Err_PHY_TimeOut );
				break;
			}
		}

		DELAY(Delay_PHYRd);
		Dat_ULONG = ReadSOC_DD( MAC_PHYBASE + 0x64 ) & 0xffff;
	} 
	else {
		WriteSOC_DD( MAC_PHYBASE + 0x60, MDC_Thres | MAC_PHYRd | (PHY_ADR << 16) | ((adr & 0x1f) << 21) );

		while ( ReadSOC_DD( MAC_PHYBASE + 0x60 ) & MAC_PHYRd ) {
			if ( ++timeout > TIME_OUT_PHY_RW ) {
#ifdef SLT_DOS
                if ( !BurstEnable ) 
				    fprintf( fp_log, "[PHY-Read] Time out: %08lx\n", ReadSOC_DD( MAC_PHYBASE + 0x60 ) );
#endif
                FindErr( Err_PHY_TimeOut );
				break;
			}
		}

		DELAY( Delay_PHYRd );
		Dat_ULONG = ReadSOC_DD( MAC_PHYBASE + 0x64 ) >> 16;
	}

	if ( DbgPrn_PHYRW ) 
	    printf ("[Rd ]%02d: %04lx\n", adr, Dat_ULONG );
	    
	return( Dat_ULONG );
} // End ULONG phy_read (int adr)

//------------------------------------------------------------
void phy_Read_Write (int adr, ULONG clr_mask, ULONG set_mask) {
	if ( DbgPrn_PHYRW ) 
	    printf ("[RW ]%02d: clr:%04lx: set:%04lx\n", adr, clr_mask, set_mask);
	phy_write(adr, ((phy_read(adr) & (~clr_mask)) | set_mask));
}

//------------------------------------------------------------
void phy_out ( int adr ) {
	printf ("%02d: %04lx\n", adr, phy_read(adr));
}

//------------------------------------------------------------
//void phy_outchg ( int adr ) {
//    ULONG	PHY_valold = 0;
//    ULONG	PHY_val;
//
//	while (1) {
//		PHY_val = phy_read(adr);
//		if (PHY_valold != PHY_val) {
//			printf ("%02d: %04lx\n", adr, PHY_val);
//			PHY_valold = PHY_val;
//		}
//	}
//}

//------------------------------------------------------------
void phy_dump (char *name) {
	int	index;

	printf ("[%s][%d]----------------\n", name, PHY_ADR);
	for (index = 0; index < 32; index++) {
		printf ("%02d: %04lx ", index, phy_read(index));
		
		if ((index % 8) == 7) 
		    printf ("\n");
	}
}

//------------------------------------------------------------
void phy_id (BYTE option) {
    
    ULONG	reg_adr;
    CHAR    PHY_ADR_org;
    FILE_VAR
    
    GET_OBJ( option )

    PHY_ADR_org = PHY_ADR;
	for (PHY_ADR = 0; PHY_ADR < 32; PHY_ADR++) {

		PRINT(OUT_OBJ "[%02d] ", PHY_ADR);

        for (reg_adr = 2; reg_adr <= 3; reg_adr++)
			PRINT(OUT_OBJ "%ld:%04lx ", reg_adr, phy_read(reg_adr));

		if ((PHY_ADR % 4) == 3) 
		    PRINT(OUT_OBJ "\n");
	}
	PHY_ADR = PHY_ADR_org;
}


//------------------------------------------------------------
void phy_delay (int dt) {
    DELAY( dt );
}

//------------------------------------------------------------
// PHY IC
//------------------------------------------------------------
void phy_basic_setting (int loop_phy) {
	phy_Read_Write(0,  0x7140, PHY_00h); //clr set
	if ( DbgPrn_PHYRW ) 
	    printf ("[Set]00: %04lx\n", phy_read( PHY_REG_BMCR ));
}

//------------------------------------------------------------
void phy_Wait_Reset_Done (void) {
	int timeout = 0;
	
	while (  phy_read( PHY_REG_BMCR ) & 0x8000 ) {
		if (DbgPrn_PHYRW) 
		    printf ("00: %04lx\n", phy_read( PHY_REG_BMCR ));
		    
		if (++timeout > TIME_OUT_PHY_Rst) {
#ifdef SLT_DOS
            if (!BurstEnable) fprintf(fp_log, "[PHY-Reset] Time out: %08lx\n", ReadSOC_DD(MAC_PHYBASE+0x60));
#endif
            FindErr(Err_PHY_TimeOut);
			break;
		}
	}//wait Rst Done

	if (DbgPrn_PHYRW) printf ("[Clr]00: %04lx\n", phy_read( PHY_REG_BMCR ));
	DELAY(Delay_PHYRst);
}

//------------------------------------------------------------
void phy_Reset (int loop_phy) {
	phy_basic_setting(loop_phy);

	phy_Read_Write(0,  0x0000, 0x8000 | PHY_00h);//clr set//Rst PHY
	phy_Wait_Reset_Done();

	phy_basic_setting(loop_phy);
	DELAY(Delay_PHYRst);
}

//------------------------------------------------------------
void recov_phy_marvell (int loop_phy) {//88E1111
	if ( BurstEnable ) {
	} 
	else if ( loop_phy ) {
	} 
	else {
		if (GSpeed_sel[0]) {
			phy_write(9, PHY_09h);

			phy_Reset(loop_phy);

			phy_write(29, 0x0007);
			phy_Read_Write(30, 0x0008, 0x0000);//clr set
			phy_write(29, 0x0010);
			phy_Read_Write(30, 0x0002, 0x0000);//clr set
			phy_write(29, 0x0012);
			phy_Read_Write(30, 0x0001, 0x0000);//clr set

			phy_write(18, PHY_12h);
		}
	}
}

//------------------------------------------------------------
void phy_marvell (int loop_phy) {//88E1111
    int	Retry;

	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Marvell] %s\n", PHY_ID2, PHY_ID3, PHYName);

	if ( BurstEnable ) {
		phy_Reset(loop_phy);
	} 
	else if ( loop_phy ) {
		phy_Reset(loop_phy);
	} 
	else {
		if ( GSpeed_sel[0] ) {
			PHY_09h = phy_read( PHY_GBCR );
			PHY_12h = phy_read( PHY_INER );
			phy_write ( 18, 0x0000         );
			phy_Read_Write(  9, 0x0000, 0x1800 );//clr set
		}                              
                                       
		phy_Reset(loop_phy);           
                                       
		if (GSpeed_sel[0]) {           
			phy_write ( 29, 0x0007         );
			phy_Read_Write( 30, 0x0000, 0x0008 );//clr set
			phy_write ( 29, 0x0010         );
			phy_Read_Write( 30, 0x0000, 0x0002 );//clr set
			phy_write ( 29, 0x0012         );
			phy_Read_Write( 30, 0x0000, 0x0001 );//clr set
		}
	}

	Retry = 0;
	do {
		PHY_11h = phy_read( PHY_SR );
	} while ( !( ( PHY_11h & 0x0400 ) | loop_phy | ( Retry++ > 20 ) ) );
}

//------------------------------------------------------------
void recov_phy_marvell0 (int loop_phy) {//88E1310
	if (BurstEnable) {
	} else if (loop_phy) {
	} else {
		if (GSpeed_sel[0]) {
			phy_write(22, 0x0006);
			phy_Read_Write(16, 0x0020, 0x0000);//clr set
			phy_write(22, 0x0000);
		}
	}
}

//------------------------------------------------------------
void phy_marvell0 (int loop_phy) {//88E1310
    int	Retry;
    
	if (DbgPrn_PHYName) 
	    printf ("--->(%04lx %04lx)[Marvell] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_write( 22, 0x0002 );

    PHY_15h = phy_read(21);
	if (PHY_15h & 0x0030) {
		printf ("\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15_2:%04lx]\n\n", PHY_15h);
#ifdef SLT_DOS
        if ( IOTiming    ) 
		    fprintf (fp_io, "\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15_2:%04lx]\n\n", PHY_15h);
		if ( !BurstEnable) 
		    fprintf (fp_log, "\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15_2:%04lx]\n\n", PHY_15h);
#endif		    
//		phy_Read_Write(21, 0x0030, 0x0000);//clr set//[5]Rx Dly, [4]Tx Dly
        phy_write(21, PHY_15h & 0xffcf); // Set [5]Rx Dly, [4]Tx Dly to 0
	}
	phy_write(22, 0x0000);

	if ( BurstEnable ) {
		phy_Reset(loop_phy);
	} 
	else if ( loop_phy ) {
		phy_write( 22, 0x0002 );
		
		if ( GSpeed_sel[0] ) {
			phy_Read_Write( 21, 0x6040, 0x0040 );//clr set
		} 
		else if ( GSpeed_sel[1] ) {
			phy_Read_Write( 21, 0x6040, 0x2000 );//clr set
		} 
		else {
			phy_Read_Write( 21, 0x6040, 0x0000 );//clr set
		}
		phy_write( 22, 0x0000 );
		phy_Reset( loop_phy );
	} 
	else {
		if ( GSpeed_sel[0] ) {
			phy_write(  22, 0x0006         );
			phy_Read_Write( 16, 0x0000, 0x0020 );//clr set
			phy_write(  22, 0x0000         );
		}

		phy_Reset(loop_phy);
	}

	Retry = 0;
	do {
		PHY_11h = phy_read( PHY_SR );
	} while (!((PHY_11h & 0x0400) | loop_phy | (Retry++ > 20)));
}

//------------------------------------------------------------
void recov_phy_marvell1 (int loop_phy) {//88E6176
    CHAR    PHY_ADR_org;
    
	PHY_ADR_org = PHY_ADR;
	for ( PHY_ADR = 16; PHY_ADR <= 22; PHY_ADR++ ) {
		if ( BurstEnable ) {
		} 
		else {
			phy_write(6, PHY_06hA[PHY_ADR-16]);//06h[5]P5 loopback, 06h[6]P6 loopback
		}
	}
	for ( PHY_ADR = 21; PHY_ADR <= 22; PHY_ADR++ ) {
		phy_write(1, 0x3); //01h[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
	}
	PHY_ADR = PHY_ADR_org;
}

//------------------------------------------------------------
void phy_marvell1 (int loop_phy) {//88E6176
//    ULONG	PHY_01h;
    CHAR    PHY_ADR_org;
    
	if (DbgPrn_PHYName) 
	    printf ("--->(%04lx %04lx)[Marvell] %s\n", PHY_ID2, PHY_ID3, PHYName);

	//The 88E6176 is switch with 7 Port(P0~P6) and the PHYAdr will be fixed at 0x10~0x16, and only P5/P6 can be connected to the MAC.
	//Therefor, the 88E6176 only can run the internal loopback.
	PHY_ADR_org = PHY_ADR;
	for ( PHY_ADR = 16; PHY_ADR <= 20; PHY_ADR++ ) {
		if ( BurstEnable ) {
		} 
		else {
			PHY_06hA[PHY_ADR-16] = phy_read( PHY_ANER );
			phy_write(6, 0x00);//06h[5]P5 loopback, 06h[6]P6 loopback
		}
	}
	
	for ( PHY_ADR = 21; PHY_ADR <= 22; PHY_ADR++ ) {
//		PHY_01h = phy_read( PHY_REG_BMSR );
//		if      (GSpeed_sel[0]) phy_write(1, (PHY_01h & 0xfffc) | 0x2);//[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
//		else if (GSpeed_sel[1]) phy_write(1, (PHY_01h & 0xfffc) | 0x1);//[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
//		else                    phy_write(1, (PHY_01h & 0xfffc)      );//[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
		if      (GSpeed_sel[0]) phy_write(1, 0x2);//01h[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
		else if (GSpeed_sel[1]) phy_write(1, 0x1);//01h[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.
		else                    phy_write(1, 0x0);//01h[1:0]00 = 10 Mbps, 01 = 100 Mbps, 10 = 1000 Mbps, 11 = Speed is not forced.

		if (BurstEnable) {
		} 
		else {
			PHY_06hA[PHY_ADR-16] = phy_read( PHY_ANER );
			if (PHY_ADR == 21) phy_write(6, 0x20);//06h[5]P5 loopback, 06h[6]P6 loopback
			else               phy_write(6, 0x40);//06h[5]P5 loopback, 06h[6]P6 loopback
		}
	}
	PHY_ADR = PHY_ADR_org;
}

//------------------------------------------------------------
void recov_phy_marvell2 (int loop_phy) {//88E1512
	if (BurstEnable) {
	} 
	else if (loop_phy) {
	} 
	else {
		if (GSpeed_sel[0]) {
			phy_write(22, 0x0006);
			phy_Read_Write(18, 0x0008, 0x0000);//clr set
			phy_write(22, 0x0000);
		}
	}
}

//------------------------------------------------------------
void phy_marvell2 (int loop_phy) {//88E1512
    int	    Retry = 0;
    ULONG   temp_reg;

	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Marvell] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_write(22, 0x0002);
	PHY_15h = phy_read(21);
	
	if ( PHY_15h & 0x0030 ) {
		printf ("\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15h_2:%04lx]\n\n", PHY_15h);
#ifdef SLT_DOS
        if (IOTiming    ) fprintf (fp_io, "\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15h_2:%04lx]\n\n", PHY_15h);
		if (!BurstEnable) fprintf (fp_log, "\n\n[Warning] Page2, Register 21, bit 4~5 must be 0 [Reg15h_2:%04lx]\n\n", PHY_15h);
#endif
        //		phy_Read_Write(21, 0x0030, 0x0000);//clr set//[5]Rx Dly, [4]Tx Dly
//		phy_write(21, PHY_15h & 0xffcf);
	}
	phy_write(22, 0x0000);

	if ( BurstEnable ) {
		phy_Reset(loop_phy);
	} 
	else if (loop_phy) {
	    // Internal loopback funciton only support in copper mode
	    // switch page 18
	    phy_write(22, 0x0012);
	    // Change mode to Copper mode
	    phy_write(20, 0x8210);
	    // do software reset
	    do {
	        temp_reg = phy_read( 20 );
	    } while ( ( (temp_reg & 0x8000) == 0x8000 ) & (Retry++ < 20) );
	    
	    // switch page 2
		phy_write(22, 0x0002);
		if (GSpeed_sel[0]) {
			phy_Read_Write(21, 0x2040, 0x0040);//clr set
		} 
		else if (GSpeed_sel[1]) {
			phy_Read_Write(21, 0x2040, 0x2000);//clr set
		} 
		else {
			phy_Read_Write(21, 0x2040, 0x0000);//clr set
		}
		phy_write(22, 0x0000);

		phy_Reset(loop_phy);
	} 
	else {
		if (GSpeed_sel[0]) {
			phy_write(22, 0x0006);
			phy_Read_Write(18, 0x0000, 0x0008);//clr set
			phy_write(22, 0x0000);
		}

		phy_Reset(loop_phy);
	}

	Retry = 0;
	do {
		PHY_11h = phy_read( PHY_SR );
	} while (!((PHY_11h & 0x0400) | loop_phy | (Retry++ > 20)));
}

//------------------------------------------------------------
void phy_broadcom (int loop_phy) {//BCM5221
	if (DbgPrn_PHYName) 
	    printf ("--->(%04lx %04lx)[Broadcom] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);

	if (IEEETesting) {
		if (IOTimingBund_arg == 0) {
			phy_write(25, 0x1f01);//Force MDI  //Measuring from channel A
		} 
		else {
			phy_Read_Write(24, 0x0000, 0x4000);//clr set//Force Link
//			phy_write( 0, PHY_00h);
//			phy_write(30, 0x1000);
		}
	}
}

//------------------------------------------------------------
void recov_phy_broadcom0 (int loop_phy) {//BCM54612

    // Need to do it for AST2400
    phy_write(0x1C, 0x8C00); // Disable GTXCLK Clock Delay Enable
    phy_write(0x18, 0xF0E7); // Disable RGMII RXD to RXC Skew            

    if (BurstEnable) {
	} 
	else if (loop_phy) {
		phy_write( 0, PHY_00h);
	} 
	else {
		phy_write(0x00, PHY_00h);
		phy_write(0x09, PHY_09h);
		phy_write(0x18, PHY_18h);
	}
}

//------------------------------------------------------------
void phy_broadcom0 (int loop_phy) {//BCM54612
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Broadcom] %s\n", PHY_ID2, PHY_ID3, PHYName);

    // Need to do it for AST2400
    phy_write(0x1C, 0x8C00); // Disable GTXCLK Clock Delay Enable
    phy_write(0x18, 0xF0E7); // Disable RGMII RXD to RXC Skew            
        
    // Backup org value
    // read reg 18H, Page 103, BCM54612EB1KMLG_Spec.pdf
    phy_write(0x18, 0x7007);
    PHY_18h = phy_read(0x18);
       
    PHY_00h = phy_read( PHY_REG_BMCR );
    PHY_09h = phy_read( PHY_GBCR );   
    
	if ( BurstEnable ) {
		phy_basic_setting(loop_phy);
	} 
	else if (loop_phy) {
		phy_basic_setting(loop_phy);
   
    // Enable Internal Loopback mode
    // Page 58, BCM54612EB1KMLG_Spec.pdf
    phy_write(0x0, 0x5140);
		DELAY(Delay_PHYRst);
    /* Only 1G Test is PASS, 100M and 10M is false @20130619 */     

// Waiting for BCM FAE's response
//        if (GSpeed_sel[0]) {
//            // Speed 1G
//            // Enable Internal Loopback mode
//            // Page 58, BCM54612EB1KMLG_Spec.pdf
//            phy_write(0x0, 0x5140);
//        } 
//        else if (GSpeed_sel[1]) {
//            // Speed 100M
//            // Enable Internal Loopback mode
//            // Page 58, BCM54612EB1KMLG_Spec.pdf
//            phy_write(0x0, 0x7100);
//            phy_write(0x1E, 0x1000);
//        } 
//        else if (GSpeed_sel[2]) {
//            // Speed 10M
//            // Enable Internal Loopback mode
//            // Page 58, BCM54612EB1KMLG_Spec.pdf
//            phy_write(0x0, 0x5100);
//            phy_write(0x1E, 0x1000);
//        }
//            
//		DELAY(Delay_PHYRst);
    } 
	else {
      
		if (GSpeed_sel[0]) {
            // Page 60, BCM54612EB1KMLG_Spec.pdf
            // need to insert loopback plug
			phy_write( 9, 0x1800);
			phy_write( 0, 0x0140);
            phy_write( 0x18, 0x8400); // Enable Transmit test mode
		} else if (GSpeed_sel[1]) {
            // Page 60, BCM54612EB1KMLG_Spec.pdf
            // need to insert loopback plug
			phy_write( 0, 0x2100);
            phy_write( 0x18, 0x8400); // Enable Transmit test mode            
		} else {
            // Page 60, BCM54612EB1KMLG_Spec.pdf
            // need to insert loopback plug
			phy_write( 0, 0x0100);
            phy_write( 0x18, 0x8400); // Enable Transmit test mode                               
		}
	}
}

//------------------------------------------------------------
void phy_realtek (int loop_phy) {//RTL8201N
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);
}

//------------------------------------------------------------
//internal loop 100M: Don't support
//internal loop 10M : no  loopback stub
void phy_realtek0 (int loop_phy) {//RTL8201E
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);

//	phy_Read_Write(25, 0x2800, 0x0000);//clr set
//	printf("Enable phy output RMII clock\n");
	if (IEEETesting) {
		phy_write(31, 0x0001);
		if (IOTimingBund_arg == 0) {
			phy_write(25, 0x1f01);//Force MDI  //Measuring from channel A
		} 
		else {
			phy_write(25, 0x1f00);//Force MDIX //Measuring from channel B
		}
		phy_write(31, 0x0000);
	}
}

//------------------------------------------------------------
void recov_phy_realtek1 (int loop_phy) {//RTL8211D
	if ( BurstEnable ) {
		if ( IEEETesting ) {
			if ( GSpeed_sel[0] ) {
				if (IOTimingBund_arg == 0) {
					//Test Mode 1
					phy_write( 31, 0x0002 );
					phy_write(  2, 0xc203 );
					phy_write( 31, 0x0000 );
					phy_write(  9, 0x0000 );
				} 
				else {
					//Test Mode 4
					phy_write( 31, 0x0000 );
					phy_write(  9, 0x0000 );
				}

				phy_write( 31, 0x0000 );
			} 
			else if ( GSpeed_sel[1] ) {
				phy_write( 23, 0x2100 );
				phy_write( 16, 0x016e );
			} 
			else {
//				phy_write( 31, 0x0006 );
//				phy_write(  0, 0x5a00 );
//				phy_write( 31, 0x0000 );
			}
		} // End if ( IEEETesting )
	} 
	else if (loop_phy) {
		if ( GSpeed_sel[0] ) {
			phy_write( 31, 0x0000 ); // new in Rev. 1.6
			phy_write(  0, 0x1140 ); // new in Rev. 1.6
			phy_write( 20, 0x8040 ); // new in Rev. 1.6
		}
	} 
	else {
		if ( GSpeed_sel[0] ) {
			phy_write( 31, 0x0001 );
			phy_write(  3, 0xdf41 );
			phy_write(  2, 0xdf20 );
			phy_write(  1, 0x0140 );
			phy_write(  0, 0x00bb );
			phy_write(  4, 0xb800 );
			phy_write(  4, 0xb000 );
                              
			phy_write( 31, 0x0000 );
//			phy_write( 26, 0x0020 ); // Rev. 1.2
			phy_write( 26, 0x0040 ); // new in Rev. 1.6
			phy_write(  0, 0x1140 );    
//			phy_write( 21, 0x0006 ); // Rev. 1.2
			phy_write( 21, 0x1006 ); // new in Rev. 1.6
			phy_write( 23, 0x2100 ); 
//		} else if ( GSpeed_sel[1] ) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0200 );
//			phy_write(  0, 0x1200 );
//		} else if ( GSpeed_sel[2] ) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0200 );
//			phy_write(  4, 0x05e1 );
//			phy_write(  0, 0x1200 );
		}
	} // End if ( BurstEnable )
} // End void recov_phy_realtek1 (int loop_phy)

//------------------------------------------------------------
//internal loop 1G  : no  loopback stub
//internal loop 100M: no  loopback stub
//internal loop 10M : no  loopback stub
void phy_realtek1 (int loop_phy) {//RTL8211D
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);
         
	if ( BurstEnable ) {
		if ( IEEETesting ) {
			if ( GSpeed_sel[0] ) {
				if (IOTimingBund_arg == 0) {
					//Test Mode 1
					phy_write( 31, 0x0002 );
					phy_write(  2, 0xc22b );
					phy_write( 31, 0x0000 );
					phy_write(  9, 0x2000 );
				} 
				else {
					//Test Mode 4
					phy_write( 31, 0x0000 );
					phy_write(  9, 0x8000 );
				}
				phy_write( 31, 0x0000 );
			} 
			else if ( GSpeed_sel[1] ) {
				if ( IOTimingBund_arg == 0 ) {
					//From Channel A
					phy_write( 23, 0xa102 );
					phy_write( 16, 0x01ae );//MDI
				} 
				else {
					//From Channel B
					phy_Read_Write( 17, 0x0008, 0x0000 ); // clr set
					phy_write(  23, 0xa102 );         // MDI
					phy_write(  16, 0x010e );
				}
			} else {
//				if ( IOTimingBund_arg == 0 ) {//Pseudo-random pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  0, 0x5a21 );
//					phy_write( 31, 0x0000 );
//				} 
//              else if ( IOTimingBund_arg == 1 ) {//ˇ§FFˇ¨ pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  2, 0x05ee );
//					phy_write(  0, 0xff21 );
//					phy_write( 31, 0x0000 );
//				} else {//ˇ§00ˇ¨ pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  2, 0x05ee );
//					phy_write(  0, 0x0021 );
//					phy_write( 31, 0x0000 );
//				}
			}
		} 
		else {
			phy_Reset(loop_phy);
		}
	} 
	else if ( loop_phy ) {
		phy_Reset(loop_phy);
		
		if ( GSpeed_sel[0] ) {
			phy_write(20, 0x0042);//new in Rev. 1.6
		}
	} 
	else {
		if ( GSpeed_sel[0] ) {
			phy_write( 31, 0x0001 );
			phy_write(  3, 0xff41 );
			phy_write(  2, 0xd720 );
			phy_write(  1, 0x0140 );
			phy_write(  0, 0x00bb );
			phy_write(  4, 0xb800 );
			phy_write(  4, 0xb000 );
                              
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x0040 );
			phy_write( 24, 0x0008 );
                              
			phy_write( 31, 0x0000 );
			phy_write(  9, 0x0300 );
			phy_write( 26, 0x0020 );
			phy_write(  0, 0x0140 );
			phy_write( 23, 0xa101 );
			phy_write( 21, 0x0200 );
			phy_write( 23, 0xa121 );
			phy_write( 23, 0xa161 );
			phy_write(  0, 0x8000 );
			phy_Wait_Reset_Done();
			phy_delay(200); // new in Rev. 1.6
//		} 
//      else if ( GSpeed_sel[1] ) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0000 );
//			phy_write(  4, 0x0061 );
//			phy_write(  0, 0x1200 );
//			phy_delay(5000);
//		} 
//      else if (GSpeed_sel[2]) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0000 );
//			phy_write(  4, 0x05e1 );
//			phy_write(  0, 0x1200 );
//			phy_delay(5000);
		} 
		else {
			phy_Reset(loop_phy);
		}
	}
} // End void phy_realtek1 (int loop_phy)

//------------------------------------------------------------
void recov_phy_realtek2 (int loop_phy) {//RTL8211E
	if ( BurstEnable ) {
		if ( IEEETesting ) {
			phy_write( 31, 0x0005 );
			phy_write(  5, 0x8b86 );
			phy_write(  6, 0xe201 );
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x0020 );
			phy_write( 21, 0x1108 );
			phy_write( 31, 0x0000 );

			if ( GSpeed_sel[0] ) {
				phy_write( 31, 0x0000 );
				phy_write(  9, 0x0000 );
			} 
			else if ( GSpeed_sel[1] ) {
				phy_write( 31, 0x0007 );
				phy_write( 30, 0x002f );
				phy_write( 23, 0xd88f );
				phy_write( 30, 0x002d );
				phy_write( 24, 0xf050 );
				phy_write( 31, 0x0000 );
				phy_write( 16, 0x006e );
			} 
			else {
			}
		} 
		else {
		}
	} 
	else if (loop_phy) {
	} 
	else {
		if (GSpeed_sel[0]) {
			//Rev 1.5  //not stable
//			phy_write( 31, 0x0000 );
//			phy_write(  0, 0x8000 );
//			phy_Wait_Reset_Done();
//			phy_delay(30);
//			phy_write( 23, 0x2160 );
//			phy_write( 31, 0x0007 );
//			phy_write( 30, 0x0040 );
//			phy_write( 24, 0x0004 );
//			phy_write( 24, 0x1a24 );
//			phy_write( 25, 0xfd00 );
//			phy_write( 24, 0x0000 );
//			phy_write( 31, 0x0000 );
//			phy_write(  0, 0x1140 );
//			phy_write( 26, 0x0040 );
//			phy_write( 31, 0x0007 );
//			phy_write( 30, 0x002f );
//			phy_write( 23, 0xd88f );
//			phy_write( 30, 0x0023 );
//			phy_write( 22, 0x0300 );
//			phy_write( 31, 0x0000 );
//			phy_write( 21, 0x1006 );
//			phy_write( 23, 0x2100 );
/**/
			//Rev 1.6
			phy_write( 31, 0x0000 );
			phy_write(  0, 0x8000 );
			phy_Wait_Reset_Done();
			phy_delay(30);
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x0042 );
			phy_write( 21, 0x0500 );
			phy_write( 31, 0x0000 );
			phy_write(  0, 0x1140 );
			phy_write( 26, 0x0040 );
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x002f );
			phy_write( 23, 0xd88f );
			phy_write( 30, 0x0023 );
			phy_write( 22, 0x0300 );
			phy_write( 31, 0x0000 );
			phy_write( 21, 0x1006 );
			phy_write( 23, 0x2100 );
/**/
//		} else if (GSpeed_sel[1]) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0200 );
//			phy_write(  0, 0x1200 );
//		} else if (GSpeed_sel[2]) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0200 );
//			phy_write(  4, 0x05e1 );
//			phy_write(  0, 0x1200 );
		}
	}
} // End void recov_phy_realtek2 (int loop_phy)

//------------------------------------------------------------
//internal loop 1G  : no  loopback stub
//internal loop 100M: no  loopback stub
//internal loop 10M : no  loopback stub
void phy_realtek2 (int loop_phy) {//RTL8211E

	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Read_Write( 0,  0x0000, 0x8000 | PHY_00h ); // clr set // Rst PHY
	phy_Wait_Reset_Done();
	phy_delay(30);

	if ( BurstEnable ) {
		if ( IEEETesting ) {
			phy_write( 31, 0x0005 );
			phy_write(  5, 0x8b86 );
			phy_write(  6, 0xe200 );
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x0020 );
			phy_write( 21, 0x0108 );
			phy_write( 31, 0x0000 );

			if ( GSpeed_sel[0] ) {
				phy_write( 31, 0x0000 );
				
				if ( IOTimingBund_arg == 0 ) {
					phy_write( 9, 0x2000);//Test Mode 1
				} 
				else {
					phy_write( 9, 0x8000);//Test Mode 4
				}
			} 
			else if ( GSpeed_sel[1] ) {
				phy_write( 31, 0x0007 );
				phy_write( 30, 0x002f );
				phy_write( 23, 0xd818 );
				phy_write( 30, 0x002d );
				phy_write( 24, 0xf060 );
				phy_write( 31, 0x0000 );
				
				if ( IOTimingBund_arg == 0 ) {
					phy_write(16, 0x00ae);//From Channel A
				} 
				else {
					phy_write(16, 0x008e);//From Channel B
				}
			} 
			else {
			}
		} 
		else {
			phy_basic_setting(loop_phy);
			phy_delay(30);
		}
	} 
	else if (loop_phy) {
		phy_basic_setting(loop_phy);

		phy_Read_Write(0,  0x0000, 0x8000 | PHY_00h);//clr set//Rst PHY
		phy_Wait_Reset_Done();
		phy_delay(30);

		phy_basic_setting(loop_phy);
		phy_delay(30);
	} 
	else {
		if ( GSpeed_sel[0] ) {
			//Rev 1.5  //not stable
//			phy_write( 23, 0x2160 );
//			phy_write( 31, 0x0007 );
//			phy_write( 30, 0x0040 );
//			phy_write( 24, 0x0004 );
//			phy_write( 24, 0x1a24 );
//			phy_write( 25, 0x7d00 );
//			phy_write( 31, 0x0000 );
//			phy_write( 23, 0x2100 );
//			phy_write( 31, 0x0007 );
//			phy_write( 30, 0x0040 );
//			phy_write( 24, 0x0000 );
//			phy_write( 30, 0x0023 );
//			phy_write( 22, 0x0006 );
//			phy_write( 31, 0x0000 );
//			phy_write(  0, 0x0140 );
//			phy_write( 26, 0x0060 );
//			phy_write( 31, 0x0007 );
//			phy_write( 30, 0x002f );
//			phy_write( 23, 0xd820 );
//			phy_write( 31, 0x0000 );
//			phy_write( 21, 0x0206 );
//			phy_write( 23, 0x2120 );
//			phy_write( 23, 0x2160 );
/**/                          
            //Rev 1.6                     
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x0042 );
			phy_write( 21, 0x2500 );
			phy_write( 30, 0x0023 );
			phy_write( 22, 0x0006 );
			phy_write( 31, 0x0000 );
			phy_write(  0, 0x0140 );
			phy_write( 26, 0x0060 );
			phy_write( 31, 0x0007 );
			phy_write( 30, 0x002f );
			phy_write( 23, 0xd820 );
			phy_write( 31, 0x0000 );
			phy_write( 21, 0x0206 );
			phy_write( 23, 0x2120 );
			phy_write( 23, 0x2160 );
			phy_delay(300);
/**/
//		} 
//      else if ( GSpeed_sel[1] ) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0000 );
//			phy_write(  4, 0x0061 );
//			phy_write(  0, 0x1200 );
//			phy_delay(5000);
//		} 
//      else if ( GSpeed_sel[2] ) {//option
//			phy_write( 31, 0x0000 );
//			phy_write(  9, 0x0000 );
//			phy_write(  4, 0x05e1 );
//			phy_write(  0, 0x1200 );
//			phy_delay(5000);
		} 
		else {
			phy_basic_setting(loop_phy);
			phy_delay(150);
		}
	}
} // End void phy_realtek2 (int loop_phy)

//------------------------------------------------------------
void recov_phy_realtek3 (int loop_phy) {//RTL8211C
	if ( BurstEnable ) {
		if ( IEEETesting ) {
			if ( GSpeed_sel[0] ) {
				phy_write(  9, 0x0000 );
			} 
			else if ( GSpeed_sel[1] ) {
				phy_write( 17, PHY_11h );
				phy_write( 14, 0x0000  );
				phy_write( 16, 0x00a0  );
			} 
			else {
//				phy_write( 31, 0x0006 );
//				phy_write(  0, 0x5a00 );
//				phy_write( 31, 0x0000 );
			}
		} 
		else {
		}
	} 
	else if (loop_phy) {
		if ( GSpeed_sel[0] ) {
			phy_write( 11, 0x0000 );
		}
		phy_write( 12, 0x1006 );
	} 
	else {
		if ( GSpeed_sel[0] ) {
			phy_write( 31, 0x0001 );
			phy_write(  4, 0xb000 );
			phy_write(  3, 0xff41 );
			phy_write(  2, 0xdf20 );
			phy_write(  1, 0x0140 );
			phy_write(  0, 0x00bb );
			phy_write(  4, 0xb800 );
			phy_write(  4, 0xb000 );
                              
			phy_write( 31, 0x0000 );
			phy_write( 25, 0x8c00 );
			phy_write( 26, 0x0040 );
			phy_write(  0, 0x1140 );
			phy_write( 14, 0x0000 );
			phy_write( 12, 0x1006 );
			phy_write( 23, 0x2109 );
		}
	}
}

//------------------------------------------------------------
void phy_realtek3 (int loop_phy) {//RTL8211C
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);

	if ( BurstEnable ) {
		if ( IEEETesting ) {
			if ( GSpeed_sel[0] ) {
				if ( IOTimingBund_arg == 0 ) {   //Test Mode 1
					phy_write( 9, 0x2000);
				} 
				else if (IOTimingBund_arg == 1) {//Test Mode 2
					phy_write( 9, 0x4000);
				} 
				else if (IOTimingBund_arg == 2) {//Test Mode 3
					phy_write( 9, 0x6000);
				} 
				else {                           //Test Mode 4
					phy_write( 9, 0x8000);
				}
			} 
			else if ( GSpeed_sel[1] ) {
				PHY_11h = phy_read( PHY_SR );
				phy_write( 17, PHY_11h & 0xfff7 );
				phy_write( 14, 0x0660 );
				
				if ( IOTimingBund_arg == 0 ) {
					phy_write( 16, 0x00a0 );//MDI  //From Channel A
				} 
				else {
					phy_write( 16, 0x0080 );//MDIX //From Channel B
				}
			} 
			else {
//				if (IOTimingBund_arg == 0) {//Pseudo-random pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  0, 0x5a21 );
//					phy_write( 31, 0x0000 );
//				} 
//              else if (IOTimingBund_arg == 1) {//ˇ§FFˇ¨ pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  2, 0x05ee );
//					phy_write(  0, 0xff21 );
//					phy_write( 31, 0x0000 );
//				} 
//              else {//ˇ§00ˇ¨ pattern
//					phy_write( 31, 0x0006 );
//					phy_write(  2, 0x05ee );
//					phy_write(  0, 0x0021 );
//					phy_write( 31, 0x0000 );
//				}
			}
		} 
		else {
			phy_Reset(loop_phy);
		}
	} 
	else if (loop_phy) {
		phy_write( 0, 0x9200);
		phy_Wait_Reset_Done();
		phy_delay(30);

		phy_write( 17, 0x401c );
		phy_write( 12, 0x0006 );
		
		if ( GSpeed_sel[0] ) {
			phy_write(11, 0x0002);
		} 
		else {
			phy_basic_setting(loop_phy);
		}
	} 
	else {
		if (GSpeed_sel[0]) {
			phy_write( 31, 0x0001 );
			phy_write(  4, 0xb000 );
			phy_write(  3, 0xff41 );
			phy_write(  2, 0xd720 );
			phy_write(  1, 0x0140 );
			phy_write(  0, 0x00bb );
			phy_write(  4, 0xb800 );
			phy_write(  4, 0xb000 );
                              
			phy_write( 31, 0x0000 );
			phy_write( 25, 0x8400 );
			phy_write( 26, 0x0020 );
			phy_write(  0, 0x0140 );
			phy_write( 14, 0x0210 );
			phy_write( 12, 0x0200 );
			phy_write( 23, 0x2109 );
			phy_write( 23, 0x2139 );
		} 
		else {
			phy_Reset(loop_phy);
		}
	}
} // End void phy_realtek3 (int loop_phy)

//------------------------------------------------------------
void phy_realtek4 (int loop_phy) {//RTL8201F
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Realtek] %s\n", PHY_ID2, PHY_ID3, PHYName);

	if ( BurstEnable ) {
		if ( IEEETesting ) {
			phy_write( 31, 0x0004 );
			phy_write( 16, 0x4077 );
			phy_write( 21, 0xc5a0 );
			phy_write( 31, 0x0000 );

			if ( GSpeed_sel[1] ) {
				phy_write(  0, 0x8000 ); // Reset PHY
				phy_write( 24, 0x0310 ); // Disable ALDPS
				
				if ( IOTimingBund_arg == 0 ) {
					phy_write( 28, 0x40c2 ); //Force MDI //From Channel A (RJ45 pair 1, 2)
				}                        
				else {                   
					phy_write( 28, 0x40c0 ); //Force MDIX//From Channel B (RJ45 pair 3, 6)
				}
				phy_write( 0, 0x2100);       //Force 100M/Full Duplex)
			}
		} else {
			phy_Reset(loop_phy);
		}
	} 
	else {
		phy_Reset(loop_phy);
	}
}

//------------------------------------------------------------
void phy_smsc (int loop_phy) {//LAN8700
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[SMSC] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);
}

//------------------------------------------------------------
void phy_micrel (int loop_phy) {//KSZ8041
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[Micrel] %s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);

//	phy_write(24,  0x0600);
}

//------------------------------------------------------------
void phy_micrel0 (int loop_phy) {//KSZ8031/KSZ8051
	if ( DbgPrn_PHYName ) printf ("--->(%04lx %04lx)[Micrel] %s\n", PHY_ID2, PHY_ID3, PHYName);

	//For KSZ8051RNL only
	//Reg1Fh[7] = 0(default): 25MHz Mode, XI, XO(pin 9, 8) is 25MHz(crystal/oscilator).
	//Reg1Fh[7] = 1         : 50MHz Mode, XI(pin 9) is 50MHz(oscilator).
	PHY_1fh = phy_read(31);
	if (PHY_1fh & 0x0080) sprintf(PHYName, "%s-50MHz Mode", PHYName);
	else                  sprintf(PHYName, "%s-25MHz Mode", PHYName);

	if (IEEETesting) {
		phy_Read_Write( 0,  0x0000, 0x8000 | PHY_00h );//clr set//Rst PHY
		phy_Wait_Reset_Done();

		phy_Read_Write( 31, 0x0000, 0x2000 );//clr set//1Fh[13] = 1: Disable auto MDI/MDI-X
		phy_basic_setting(loop_phy);
		phy_Read_Write( 31, 0x0000, 0x0800 );//clr set//1Fh[11] = 1: Force link pass

//		phy_delay(2500);//2.5 sec
	} 
	else {
		phy_Reset(loop_phy);

		//Reg16h[6] = 1         : RMII B-to-B override
		//Reg16h[1] = 1(default): RMII override
		phy_Read_Write( 22, 0x0000, 0x0042 );//clr set
	}

	if ( PHY_1fh & 0x0080 ) 
	    phy_Read_Write( 31, 0x0000, 0x0080 );//clr set//Reset PHY will clear Reg1Fh[7]
}

//------------------------------------------------------------
void recov_phy_vitesse (int loop_phy) {//VSC8601
	if ( BurstEnable ) {
//		if (IEEETesting) {
//		} else {
//		}
	} 
	else if ( loop_phy ) {
	} 
	else {
		if ( GSpeed_sel[0] ) {
			phy_write( 24, PHY_18h );
			phy_write( 18, PHY_12h );
		}
	}
}

//------------------------------------------------------------
void phy_vitesse (int loop_phy) {//VSC8601
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)[VITESSE] %s\n", PHY_ID2, PHY_ID3, PHYName);

	if ( BurstEnable ) {
		if ( IEEETesting ) {
			phy_Reset(loop_phy);
		} 
		else {
			phy_Reset(loop_phy);
		}
	} 
	else if ( loop_phy ) {
		phy_Reset(loop_phy);
	} 
	else {
		if ( GSpeed_sel[0] ) {
			PHY_18h = phy_read( 24 );
			PHY_12h = phy_read( PHY_INER );

			phy_Reset(loop_phy);

			phy_write( 24, PHY_18h | 0x0001 );
			phy_write( 18, PHY_12h | 0x0020 );
		} 
		else {
			phy_Reset(loop_phy);
		}
	}
}

//------------------------------------------------------------
void phy_default (int loop_phy) {
	if ( DbgPrn_PHYName ) 
	    printf ("--->(%04lx %04lx)%s\n", PHY_ID2, PHY_ID3, PHYName);

	phy_Reset(loop_phy);
}

//------------------------------------------------------------
// PHY Init
//------------------------------------------------------------
BOOLEAN find_phyadr (void) {
    ULONG       PHY_val;
    BOOLEAN     ret = FALSE;
    
	#ifdef  DbgPrn_FuncHeader
	    printf ("find_phyadr\n"); 
	    Debug_delay();
    #endif

	do {
	    // Check current PHY address by user setting
	    PHY_val = phy_read( PHY_REG_ID_1 );
    	if ( PHY_IS_VALID(PHY_val) ) {
    		ret = TRUE;
    		break;
    	}
    	 
    	if ( Enable_SkipChkPHY ) {
    		PHY_val = phy_read( PHY_REG_BMCR );
    		
    		if ((PHY_val & 0x8000) & Enable_InitPHY) {
    		    // PHY is reseting and need to inital PHY
                #ifndef Enable_SearchPHYID
    			break;
                #endif
    		} 
    		else {
    			ret = TRUE;
                break;    			
    		}
    	} 
    	
    	#ifdef Enable_SearchPHYID
    	// Scan PHY address from 0 to 31
        printf("Search PHY address\n");
        for ( PHY_ADR = 0; PHY_ADR < 32; PHY_ADR++ ) {
        	PHY_val = phy_read( PHY_REG_ID_1 );
        	if ( PHY_IS_VALID(PHY_val) ) {
        		ret = TRUE;
        		break;
        	}
        }
        // Don't find PHY address
        PHY_ADR = PHY_ADR_arg;
        #endif
    } while ( 0 );

	if ( ret == TRUE ) {
		if ( PHY_ADR_arg != PHY_ADR ) {

            if ( !BurstEnable ) 
			    phy_id( FP_LOG );

            phy_id( STD_OUT );
		}
	} 
	else {

		if ( !BurstEnable ) 
		    phy_id( FP_LOG );

        phy_id( STD_OUT );
		FindErr( Err_PHY_Type );
	}
	
	return ret;
} // End BOOLEAN find_phyadr (void)

//------------------------------------------------------------
char phy_chk (ULONG id2, ULONG id3, ULONG id3_mask) {
	if ((PHY_ID2 == id2) && ((PHY_ID3 & id3_mask) == (id3 & id3_mask))) 
	    return(1);
	else                                                                
	    return(0);
}

//------------------------------------------------------------
void phy_set00h (int loop_phy) {
	#ifdef  DbgPrn_FuncHeader
	    printf ("phy_set00h\n"); 
	    Debug_delay();
    #endif

	if (BurstEnable) {
		if (IEEETesting) {
			if      (GSpeed_sel[0]) PHY_00h = 0x0140;
			else if (GSpeed_sel[1]) PHY_00h = 0x2100;
			else                    PHY_00h = 0x0100;
		} 
		else {
			if      (GSpeed_sel[0]) PHY_00h = 0x1140;
			else if (GSpeed_sel[1]) PHY_00h = 0x3100;
			else                    PHY_00h = 0x1100;
		}
	} 
	else if (loop_phy) {
		if      (GSpeed_sel[0]) PHY_00h = 0x4140;
		else if (GSpeed_sel[1]) PHY_00h = 0x6100;
		else                    PHY_00h = 0x4100;
	} 
	else {
		if      (GSpeed_sel[0]) PHY_00h = 0x0140;
		else if (GSpeed_sel[1]) PHY_00h = 0x2100;
		else                    PHY_00h = 0x0100;
	}
}

//------------------------------------------------------------
void phy_sel (int loop_phy) {
	#ifdef  DbgPrn_FuncHeader
	    printf ("phy_sel\n"); 
	    Debug_delay();
    #endif

	PHY_ID2 = phy_read( PHY_REG_ID_1 );
	PHY_ID3 = phy_read( PHY_REG_ID_2 );
	phy_set00h(loop_phy);

	if      ((PHY_ID2 == 0xffff) && (PHY_ID3 == 0xffff) && !Enable_SkipChkPHY) {
	    sprintf(PHYName, "--"); 
	    FindErr(Err_PHY_Type);
	}
#ifdef Enable_CheckZeroPHYID
	else if ((PHY_ID2 == 0x0000) && (PHY_ID3 == 0x0000) && !Enable_SkipChkPHY) {
	    sprintf(PHYName, "--"); FindErr(Err_PHY_Type);
	}
#endif

	if      (phy_chk(0x0362, 0x5e6a, 0xfff0     )) {sprintf(PHYName, "BCM54612"        ); if (Enable_InitPHY) phy_broadcom0(loop_phy);}//BCM54612         1G/100/10M  RGMII
	else if (phy_chk(0x0362, 0x5d10, 0xfff0     )) {sprintf(PHYName, "BCM54616S"       ); if (Enable_InitPHY) phy_broadcom0(loop_phy);}//BCM54616A        1G/100/10M  RGMII	    
	else if (phy_chk(0x0040, 0x61e0, PHYID3_Mask)) {sprintf(PHYName, "BCM5221"         ); if (Enable_InitPHY) phy_broadcom (loop_phy);}//BCM5221             100/10M  MII, RMII
	else if (phy_chk(0x0141, 0x0dd0, 0xfff0     )) {sprintf(PHYName, "88E1512"         ); if (Enable_InitPHY) phy_marvell2 (loop_phy);}//88E1512          1G/100/10M  RGMII
	else if (phy_chk(0xff00, 0x1761, 0xffff     )) {sprintf(PHYName, "88E6176(IntLoop)"); if (Enable_InitPHY) phy_marvell1 (loop_phy);}//88E6176          1G/100/10M  2 RGMII Switch
	else if (phy_chk(0x0141, 0x0e90, 0xfff0     )) {sprintf(PHYName, "88E1310"         ); if (Enable_InitPHY) phy_marvell0 (loop_phy);}//88E1310          1G/100/10M  RGMII
	else if (phy_chk(0x0141, 0x0cc0, PHYID3_Mask)) {sprintf(PHYName, "88E1111"         ); if (Enable_InitPHY) phy_marvell  (loop_phy);}//88E1111          1G/100/10M  GMII, MII, RGMII
	else if (phy_chk(0x001c, 0xc816, 0xffff     )) {sprintf(PHYName, "RTL8201F"        ); if (Enable_InitPHY) phy_realtek4 (loop_phy);}//RTL8201F            100/10M  MII, RMII
	else if (phy_chk(0x001c, 0xc815, 0xfff0     )) {sprintf(PHYName, "RTL8201E"        ); if (Enable_InitPHY) phy_realtek0 (loop_phy);}//RTL8201E            100/10M  MII, RMII(RTL8201E(L)-VC only)
	else if (phy_chk(0x001c, 0xc912, 0xffff     )) {sprintf(PHYName, "RTL8211C"        ); if (Enable_InitPHY) phy_realtek3 (loop_phy);}//RTL8211C         1G/100/10M  RGMII
	else if (phy_chk(0x001c, 0xc914, 0xffff     )) {sprintf(PHYName, "RTL8211D"        ); if (Enable_InitPHY) phy_realtek1 (loop_phy);}//RTL8211D         1G/100/10M  GMII(RTL8211DN/RTL8211DG only), MII(RTL8211DN/RTL8211DG only), RGMII
	else if (phy_chk(0x001c, 0xc915, 0xffff     )) {sprintf(PHYName, "RTL8211E"        ); if (Enable_InitPHY) phy_realtek2 (loop_phy);}//RTL8211E         1G/100/10M  GMII(RTL8211EG only), RGMII
	else if (phy_chk(0x0000, 0x8201, PHYID3_Mask)) {sprintf(PHYName, "RTL8201N"        ); if (Enable_InitPHY) phy_realtek  (loop_phy);}//RTL8201N            100/10M  MII, RMII
	else if (phy_chk(0x0007, 0xc0c4, PHYID3_Mask)) {sprintf(PHYName, "LAN8700"         ); if (Enable_InitPHY) phy_smsc     (loop_phy);}//LAN8700             100/10M  MII, RMII
	else if (phy_chk(0x0022, 0x1555, 0xfff0     )) {sprintf(PHYName, "KSZ8031/KSZ8051" ); if (Enable_InitPHY) phy_micrel0  (loop_phy);}//KSZ8051/KSZ8031     100/10M  RMII
	else if (phy_chk(0x0022, 0x1560, 0xfff0     )) {sprintf(PHYName, "KSZ8081"         ); if (Enable_InitPHY) phy_micrel0  (loop_phy);}//KSZ8081             100/10M  RMII
	else if (phy_chk(0x0022, 0x1512, 0xfff0     )) {sprintf(PHYName, "KSZ8041"         ); if (Enable_InitPHY) phy_micrel   (loop_phy);}//KSZ8041             100/10M  RMII
	else if (phy_chk(0x0007, 0x0421, 0xfff0     )) {sprintf(PHYName, "VSC8601"         ); if (Enable_InitPHY) phy_vitesse  (loop_phy);}//VSC8601          1G/100/10M  RGMII
	else                                           {sprintf(PHYName, "default"         ); if (Enable_InitPHY) phy_default  (loop_phy);}//
}

//------------------------------------------------------------
void recov_phy (int loop_phy) {
	#ifdef  DbgPrn_FuncHeader
	    printf ("recov_phy\n"); 
	    Debug_delay();
    #endif

	if      (phy_chk(0x0362, 0x5e6a, 0xfff0     )) recov_phy_broadcom0(loop_phy);//BCM54612  1G/100/10M  RGMII
	else if (phy_chk(0x0362, 0x5d10, 0xfff0     )) recov_phy_broadcom0(loop_phy);//BCM54616A 1G/100/10M  RGMII
	else if (phy_chk(0x0141, 0x0dd0, 0xfff0     )) recov_phy_marvell2 (loop_phy);//88E1512   1G/100/10M  RGMII
	else if (phy_chk(0xff00, 0x1761, 0xffff     )) recov_phy_marvell1 (loop_phy);//88E6176   1G/100/10M  2 RGMII Switch
	else if (phy_chk(0x0141, 0x0e90, 0xfff0     )) recov_phy_marvell0 (loop_phy);//88E1310   1G/100/10M  RGMII
	else if (phy_chk(0x0141, 0x0cc0, PHYID3_Mask)) recov_phy_marvell  (loop_phy);//88E1111   1G/100/10M  GMII, MII, RGMII
	else if (phy_chk(0x001c, 0xc914, 0xffff     )) recov_phy_realtek1 (loop_phy);//RTL8211D  1G/100/10M  GMII(RTL8211DN/RTL8211DG only), MII(RTL8211DN/RTL8211DG only), RGMII
	else if (phy_chk(0x001c, 0xc915, 0xffff     )) recov_phy_realtek2 (loop_phy);//RTL8211E  1G/100/10M  GMII(RTL8211EG only), RGMII
	else if (phy_chk(0x001c, 0xc912, 0xffff     )) recov_phy_realtek3 (loop_phy);//RTL8211C  1G/100/10M  RGMII
	else if (phy_chk(0x0007, 0x0421, 0xfff0     )) recov_phy_vitesse  (loop_phy);//VSC8601   1G/100/10M  RGMII
}

//------------------------------------------------------------
void init_phy (int loop_phy) {
	#ifdef  DbgPrn_FuncHeader
	    printf ("init_phy\n"); 
	    Debug_delay();
    #endif
	
	sprintf( PHYID, "PHY%d", SelectMAC + 1 );

	if ( DbgPrn_PHYInit ) 
	    phy_dump( PHYID );

	if ( find_phyadr() == TRUE ) 
	    phy_sel( loop_phy );

	if ( DbgPrn_PHYInit )
	    phy_dump( PHYID );
}


OpenPOWER on IntegriCloud