summaryrefslogtreecommitdiff
path: root/libmii.c
blob: 49e793473e21467cf2a5471179ff6ac3ac39adeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
/* libmii.c: MII diagnostic and setup library.

	Copyright 1997-2003 by Donald Becker.
	This version released under the Gnu General Public License,
	incorporated herein by reference.
	This source code may be distributed without modification using the
	existing notice.  Any modification to this source code must include a
	full notice as described in the GPL
	Contact the author for use under other terms.

	The author may be reached as becker@scyld.com, or C/O
	 Scyld Computing Corporation
	 914 Bay Ridge Road, Suite 220
	 Annapolis MD 21403

   References
	http://www.scyld.com/expert/NWay.html
	http://www.national.com/pf/DP/DP83840A.html
*/

static const char version_msg[] =
"libmii.c:v2.11 2/28/2005  Donald Becker (becker@scyld.com)\n"
" http://www.scyld.com/diag/index.html\n";

/* This library exports the following functions:
    IOADDR: A token passed to the mdio_read() function.
    PHY_ID: The MII transceiver address, passed uninterpreted to mdio_read().
*/
void show_mii_details(long ioaddr, int phy_id);
int monitor_mii(long ioaddr, int phy_id);

/* This library expects to be able to call the following functions: */
extern int mdio_read(long ioaddr, int phy_id, int mii_reg_num);
extern void mdio_write(long ioaddr, int phy_id, int mii_reg_num, int value);

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#include <sys/types.h>

typedef u_int32_t u32;
typedef u_int16_t u16;
typedef u_int8_t u8;

static const char *media_names[] = {
	"10baseT", "10baseT-FD", "100baseTx", "100baseTx-FD", "100baseT4",
	"Pause", "AsymPause", 0,
};

static void ns83843(long ioaddr, int phy_id);
static void qs6612(long ioaddr, int phy_id);
static void smsc83c180(long ioaddr, int phy_id);
static void tdk78q2120(long ioaddr, int phy_id);
static void davicom_dm9101(long ioaddr, int phy_id);
static void intel_i553(long ioaddr, int phy_id);
static void enablesemi(long ioaddr, int phy_id);
static void amd_pna(long ioaddr, int phy_id);
static void amd_tx(long ioaddr, int phy_id);
static void admtek(long ioaddr, int phy_id);
static void lu3x31(long ioaddr, int phy_id);
static void myson981(long ioaddr, int phy_id);
static void via_tahoe(long ioaddr, int phy_id);
static void via_vt6103(long ioaddr, int phy_id);
static void via_vt6105(long ioaddr, int phy_id);
static void intel(long ioaddr, int phy_id);
static void mv88x3310_xs(long ioaddr, int phy_id);
static void mv88x3310(long ioaddr, int phy_id);
static void mv88e1111(long ioaddr, int phy_id);
static void mv88e151x(long ioaddr, int phy_id);

enum {
	MDIO_MMD_PMAPMD		= 1,
	MDIO_MMD_WIS		= 2,
	MDIO_MMD_PCS		= 3,
	MDIO_MMD_PHY_XS		= 4,
	MDIO_MMD_DTE_XS		= 5,
	MDIO_MMD_TC		= 6,
	MDIO_MMD_AN		= 7,
	MDIO_MMD_VENDOR1	= 30,
	MDIO_MMD_VENDOR2	= 31,

	MMD_CTRL1		= 0,
	MMD_STAT1		= 1,
	MMD_DEVID1		= 2,
	MMD_DEVID2		= 3,
	MMD_SPEED		= 4,
	MMD_DEVPKG1		= 5,
	MMD_DEVPKG2		= 6,
	MMD_CTRL2		= 7,
	MMD_STAT2		= 8,
	MMD_PKGID1		= 14,
	MMD_PKGID2		= 15,

	MMD_PMA_EXT_ABL		= 11,
	MMD_PMA_40G100G_EXT_ABL	= 13,
	MMD_PMA_EEE_CAPABILITY	= 16,
	MMD_PMA_25G_EXT_ABL	= 19,
	MMD_PMA_NBASE_EXT_ABL	= 21,
	MMD_PMA_10GBT_STATUS	= 129,
	MMD_PMA_10GBT_SWAPPOL	= 130,

	MMD_PCS_EEE_CTRL_CAPAB	= 20,
	MMD_PCS_EEE_WAKE_ERR	= 22,
	MMD_PCS_BASERT_STAT1	= 32,
	MMD_PCS_BASERT_STAT2	= 33,

	MMD_PHY_XS_EEE_CAPAB	= 20,
	MMD_PHY_XS_EEE_WAKE_ERR	= 22,
	MMD_PHY_XS_LANE_STATUS	= 24,

	MMD_DTE_XS_EEE_CAPAB	= 20,
	MMD_DTE_XS_EEE_WAKE_ERR	= 22,

	MMD_AN_BASE_ADV0	= 16,
	MMD_AN_BASE_ADV1	= 17,
	MMD_AN_BASE_ADV2	= 18,
	MMD_AN_BASE_LPA0	= 19,
	MMD_AN_BASE_LPA1	= 20,
	MMD_AN_BASE_LPA2	= 21,
	MMD_AN_XNP_ADV0		= 22,
	MMD_AN_XNP_ADV1		= 23,
	MMD_AN_XNP_ADV2		= 24,
	MMD_AN_XNP_LPA0		= 25,
	MMD_AN_XNP_LPA1		= 26,
	MMD_AN_XNP_LPA2		= 27,
	MMD_AN_MULTI_GBT_CTRL	= 32,
	MMD_AN_MULTI_GBT_STAT	= 33,
	MMD_AN_BASER_STAT	= 48,
	MMD_AN_EEE_ADV		= 60,
	MMD_AN_EEE_LPA		= 61,
};

#define BIT(x)	(1  << (x))

static const char *mmd_name[32] = {
	[MDIO_MMD_PMAPMD]	= "PMA/PMD",
	[MDIO_MMD_WIS]		= "WIS",
	[MDIO_MMD_PCS]		= "PCS",
	[MDIO_MMD_PHY_XS]	= "PHY XS",
	[MDIO_MMD_DTE_XS]	= "DTE XS",
	[MDIO_MMD_TC]		= "TC",
	[MDIO_MMD_AN]		= "AN",
	[MDIO_MMD_VENDOR1]	= "Vendor Specific 1",
	[MDIO_MMD_VENDOR2]	= "Vendor Specific 2",
};

struct phy {
	long ioaddr;
	int phy_id;
	int devad;
	unsigned int page;
	unsigned int offset;
	int (*mdio_read)(const struct phy *phy, unsigned int reg);
};

#define DEFINE_C22_PHY(name, addr, id)		\
	struct phy name = {			\
		.ioaddr = addr,			\
		.phy_id = id,			\
		.mdio_read = __mdio_read_phy,	\
	}
#define DEFINE_C45_PHY(name, addr, id, __devad)	\
	struct phy name = {			\
		.ioaddr = addr,			\
		.phy_id = id,			\
		.devad  = __devad,		\
		.mdio_read = __mdio_read_phy,	\
	}

struct c22_decoder {
	void (*show_advert)(struct phy *phy, u16 *regs);
	void (*show_lpar)(struct phy *phy, u16 *regs);
};

struct mii_partnum {
	u32	 phy_id;		/* Vendor ID */
	u32      msk_id;		/* Vendor ID mask */
	const char *vendor;		/* Vendor name. */
	void (*(func))(long xcvr_if, int phy_id);/* Function to emit more info. */
} static oui_map[] = {
#define OUI(a,b,c) ((a) << 26 | (b) << 18 | (c) << 10), 0xfffffc00
	/* Table sorted in numerical order, but with regard to the mask */
	{0x00000000, 0xffffffff, "Unknown transceiver type"},
	{0x00006b91, 0xffffffff, "AMD 79c901A.1 HomePNA", amd_pna },
	{0x00006b92, 0xffffffff, "AMD 79c901A.2 HomePNA", amd_pna },
	{0x00006b93, 0xffffffff, "AMD 79c901A.3 HomePNA", amd_pna },
	{0x00007b71, 0xffffffff, "AMD 79c901A.3 10baseT", amd_tx },
	{0x00221720, 0xffffffff, "Alta/Kendin Sundance"},
	{OUI(0x00, 0x08, 0x85),  "Alta/Kendin Sundance"},
	{0x002b09a0, 0xfffffff0, "Marvell Semiconductor 88X3310", mv88x3310 },
	{0x002bdc00, 0xfffffc00, "Broadcom BCM63XX"},
	{0x00406000, 0xfffffc00, "Broadcom BCM63XX"},
	{0x00437411, 0xffffffff, "Enable Semiconductor EL40-331", enablesemi },
	{0x004dd076, 0xffffffef, "Atheros AT8030"},
	{0x004dd074, 0xffffffef, "Atheros AT8031"},
	{0x004dd072, 0xffffffef, "Atheros AT8035"},
	{0x01018f20, 0xffffffff, "VIA Tahoe VT6103", via_vt6103 },
	{0x01018f30, 0xffffffff, "VIA Tahoe VT6104", via_tahoe },
	{0x01018f22, 0xffffffff, "VIA Rhine VT6105", via_vt6105 },
	{0x01410c60, 0xfffffff0, "Marvell Semiconductor 88E1101"},
	{0x01410c90, 0xfffffff0, "Marvell Semiconductor 88E1112"},
	{0x01410cb0, 0xfffffff0, "Marvell Semiconductor 88E1121R"},
	{0x01410cc0, 0xfffffff0, "Marvell Semiconductor 88E1111", mv88e1111 },
	{0x01410cd0, 0xfffffff0, "Marvell Semiconductor 88E1145"},
	{0x01410da0, 0xfffffff0, "Marvell Semiconductor 88X3310", mv88x3310_xs},
	{0x01410dd0, 0xfffffff0, "Marvell Semiconductor 88E151x", mv88e151x },
	{0x01410e10, 0xfffffff0, "Marvell Semiconductor 88E1118"},
	{0x01410e30, 0xfffffff0, "Marvell Semiconductor 88E1240"},
	{0x01410e40, 0xfffffff0, "Marvell Semiconductor 88E1116R"},
	{0x01410e50, 0xfffffff0, "Marvell Semiconductor 88E1149R"},
	{0x01410e60, 0xfffffff0, "Marvell Semiconductor 88E3016"},
	{0x01410e90, 0xfffffff0, "Marvell Semiconductor 88E1318S"},
	{0x01410ea0, 0xfffffff0, "Marvell Semiconductor 88E1545"},
	{0x01410eb0, 0xfffffff0, "Marvell Semiconductor 88E1540"},
	{OUI(0x00, 0x50, 0x43),  "Marvell Semiconductor (unknown type)"},
	{0x0143bc30, 0xfffffff0, "Broadcom BCM5241"},
	{0x0143bc70, 0xfffffff0, "Broadcom BCMAC131"},
	{0x0143bca0, 0xfffffff0, "Broadcom BCM5481"},
	{0x0143bcf0, 0xfffffff0, "Broadcom BCM5395"},
	{0x0143bd60, 0xfffffff0, "Broadcom BCM50610"},
	{0x0143bd70, 0xfffffff0, "Broadcom BCM50610M"},
	{0x01814410, 0xffffffff, "Quality Semiconductor QS6612", qs6612 },
	{OUI(0x00, 0x60, 0x51),  "Quality Semiconductor (unknown type)" },
	{0x0181b800, 0xffffffff, "Davicom DM9101", davicom_dm9101 },
	{OUI(0x00, 0x60, 0x6e),  "Davicom (unknown type)", davicom_dm9101 },
	{0x02821c51, 0xffffffff, "SMSC 83c180", smsc83c180 },
	{0x02a80150, 0xffffffff, "Intel 82557 series", intel },
	{0x02a80151, 0xffffffff, "Intel 82555 rev 1", intel },
	{0x02a80154, 0xffffffff, "Intel 82559 transceiver", intel },
	{OUI(0x00, 0xaa, 0x00),  "Intel 82555 series transceiver", intel },
	{0x0300e542, 0xffffffff, "TDK Semi 78Q2120 rev. 2", tdk78q2120 },
	{0x0300e543, 0xffffffff, "TDK Semi 78Q2120 rev. 3", tdk78q2120 },
	{0x0300e54b, 0xffffffff, "TDK Semi 78Q2120 rev. 11", tdk78q2120 },
	{OUI(0x00, 0xc0, 0x39),  "TDK transceiver (unknown type)", tdk78q2120 },
	{0x0302d000, 0xffffffff, "Myson MTD981", myson981 },
	{OUI(0x00, 0xc0, 0xb4),  "Myson (unknown type)", myson981 },
	{OUI(0x00, 0xf8, 0x00),  "Intel (unknown type)", intel_i553 },
	{0x20005c01, 0xffffffff, "National Semiconductor 83840A"},
	{0x20005c10, 0xffffffff, "National Semiconductor 83843", ns83843 },
	{0x600d8480, 0xfffffff0, "Broadcom BCM7439"},
	{0x600d8490, 0xfffffff0, "Broadcom BCM7366"},
	{0x600d84b0, 0xfffffff0, "Broadcom BCM7362"},
	{0x600d8510, 0xfffffff0, "Broadcom BCM7445"},
	{0x600d8650, 0xfffffff0, "Broadcom BCM7346"},
	{0x600d86b0, 0xfffffff0, "Broadcom BCM7425"},
	{0x600d8730, 0xfffffff0, "Broadcom BCM7429"},
	{0x600d8750, 0xfffffff0, "Broadcom BCM7435"},
	{0x78100000, 0xffffffff, "Level One LXT970"},
	{0x78100001, 0xffffffff, "Level One LXT971"},
	{0x78100003, 0xffffffff, "Level One LXT971A"},
	{OUI(0x1e, 0x04, 0x00),  "Level One (unknown type)"},
	{0xae025080, 0xfffffff0, "Broadcom BCM7439"},
	{0xae025090, 0xfffffff0, "Broadcom BCM7268"},
	{0xae025190, 0xfffffff0, "Broadcom BCM7260"},
	{0xae0251a0, 0xfffffff0, "Broadcom BCM7278"},
	{0xae025260, 0xfffffff0, "Broadcom BCM7364"},
	{0xae025280, 0xfffffff0, "Broadcom BCM7250"},
	{0xae0252e0, 0xfffffff0, "Broadcom BCM74371"},
	{0xae0253b0, 0xfffffff0, "Broadcom BCM7271"},

	/* Unsorted entries */
	{0x0022561b, 0xffffffff, "AdHoc Technology AH101LF", tdk78q2120 },
	{0x00225523, 0xffffffff, "Altimata Communications AC101LF", tdk78q2120 },
	{OUI(0x00, 0x10, 0xa9),  "Altimata Comm (unknown type)", tdk78q2120 },
	{OUI(0x00, 0xc0, 0xb4),  "ASIX (unknown type)",  tdk78q2120 },
	{0x00225410, 0xffffffff, "ADMtek AN983 Comet", admtek },
	{0x00225513, 0xffffffff, "ADMtek AN985 Comet", admtek },
	{OUI(0x00, 0xe0, 0x92),  "ADMtek (unknown type)", admtek },
	{0x01807641, 0xffffffff, "Lucent LU6612", qs6612 },
	{0x00437411, 0xffffffff, "Lucent LU3X31", lu3x31 },
	{OUI(0x00, 0xa0, 0x7d),  "LSI Logic (Seeq) 80225"},
	{0, },
};

static int __mdio_read_phy(const struct phy *phy, unsigned int reg)
{
	return mdio_read(phy->ioaddr, phy->phy_id | phy->devad,
			 phy->offset + reg);
}

static int mdio_read_phy(const struct phy *phy, unsigned int reg)
{
	return phy->mdio_read(phy, reg);
}

static int lookup_vendor(int reg2, int reg3)
{
	u32 id = reg2 << 16 | reg3;
	unsigned char oui_0 = id >> 26;
	unsigned char oui_1 = id >> 18;
	unsigned char oui_2 = id >> 10;
	unsigned char model = (id >> 4) & 0x3f;
	unsigned char rev   = id & 0x0f;
	int i, vendor = 0;

	printf(" Vendor ID is %2.2x:%2.2x:%2.2x:--:--:--, model %d rev. %d.\n",
	       oui_0, oui_1, oui_2, model, rev);

	for (i = 0; oui_map[i].vendor; i++)
		if (((oui_map[i].phy_id ^ id) & oui_map[i].msk_id) == 0) {
			printf("   Vendor/Part: %s.\n", oui_map[i].vendor);
			vendor = i;
			break;
		}
	if (oui_map[i].vendor == NULL)
		printf("   No specific information is known about this transceiver"
			   " type.\n");

	return vendor;
}

struct msg_tbl { int bitmask;  char *msg; };

static void msg_if_set(const int val, const struct msg_tbl msg_tbl[])
{
	int i;
	for (i = 0; msg_tbl[i].bitmask; i++)
		if (msg_tbl[i].bitmask & val)
			printf(" %s\n", msg_tbl[i].msg);
}

static void msg_if_set_fmt(const int val, const struct msg_tbl msg_tbl[],
						   const char *fmt)
{
	int i;
	for (i = 0; msg_tbl[i].bitmask; i++)
		if (msg_tbl[i].bitmask & val)
			printf(fmt, msg_tbl[i].msg);
}

static u16 mii_val[64], c45_an_ctrl;

void read_mii_regs(long ioaddr, int phy_id, unsigned int first, unsigned int num_regs)
{
	int mii_reg;

	for (mii_reg = 0; mii_reg < num_regs; mii_reg++) {
		int val = mdio_read(ioaddr, phy_id, first + mii_reg);
		mii_val[mii_reg] = val < 0 ? 0 : val;
	}
}

static void show_mii_regs(int num_regs)
{
	int mii_reg;

	for (mii_reg = 0; mii_reg < num_regs; mii_reg++)
		printf("%s %4.4x", (mii_reg % 8) == 0 ? "\n  " : "",
			   mii_val[mii_reg]);
}

struct bit_decoder {
	u16 mask;
	u16 val;
	const char *str;
};

static const char **bit_decoder(const char **str, u16 val,
				const struct bit_decoder *decoder)
{
	int i;

	for (i = 0; decoder[i].str; i++)
		if ((val & decoder[i].mask) == decoder[i].val)
			*str++ = decoder[i].str;
	*str = NULL;

	return str;
}

struct multi_bit_decoder {
	unsigned int offset;
	const struct bit_decoder *bits;
};

static const char **multi_bit_decoder(const char **str, const u16 *vals,
				      const struct multi_bit_decoder *mdec)
{
	const char **p = str;
	int i;

	for (i = 0; mdec[i].bits; i++)
		p = bit_decoder(p, vals[mdec[i].offset], mdec[i].bits);

	return p;
}

#if 0
static char *strconcat(const char *separator, const char **strings)
{
	size_t length, sep_length = strlen(separator);
	char *string, *p;
	int i;

	if (!strings[0])
		return NULL;

	for (i = 0, length = 0; strings[i]; i++)
		length += strlen(strings[i]);
	length += (i - 1) * sep_length;

	string = malloc(length);
	if (!string)
		return NULL;

	for (i = 0, p = string; strings[i]; i++) {
		if (i) {
			strcpy(p, separator);
			p += sep_length;
		}
		strcpy(p, strings[i]);
		p += strlen(p);
	}

	return string;
}
#endif

static void print_strings(const char **strs, const char *prefix,
			  const char *separator)
{
	int need_separator = 0;
	int need_prefix = 1;
	int i;

	for (i = 0; strs[i]; i++) {
		const char *pfx = "";
		const char *str = strs[i];

		if (need_prefix) {
			pfx = prefix;
			need_prefix = 0;
		} else if (need_separator) {
			pfx = separator;
		}

		printf("%s%s", pfx, str);
		if (str[strlen(str) - 1] == '\n')
			need_prefix = 1;
		need_separator = 1;
	}
	if (!need_separator || !need_prefix)
		printf(".\n");
}

static void show_mii_bits(u16 val, const struct bit_decoder *decoder,
			  const char *prefix, const char *separator)
{
	const char *strings[64];

	bit_decoder(strings, val, decoder);
	print_strings(strings, prefix, separator);
}

static void show_mii_reg(const char *name, u16 val,
			 const struct bit_decoder *decoder,
			 const char *prefix, const char *separator)
{
	printf(" %s %4.4x", name, val);
	show_mii_bits(val, decoder, prefix, separator);
}

static void show_mii_multibits(u16 *vals,
			       const struct multi_bit_decoder *decoder,
			       const char *prefix, const char *separator)
{
	const char *strings[64];

	multi_bit_decoder(strings, vals, decoder);
	print_strings(strings, prefix, separator);
}

static const char *protocol(unsigned int proto)
{
	if (proto == 1)
		return "IEEE 802.3 CSMA/CD protocol";
	else
		return "Using an unknown (non 802.3) encapsulation";
}

static void show_protocol(unsigned int proto)
{
	printf("   %s.\n", protocol(proto));
}


/* AN advertisements etc */
// Clause 22 base page advert
static const struct bit_decoder c22_base_advert[] = {
	{ BIT(11), BIT(11), "AsymPause" },
	{ BIT(10), BIT(10), "Pause" },
	{ BIT(9),  BIT(9),  "100baseT4" },
	{ BIT(8),  BIT(8),  "100baseTx-FD" },
	{ BIT(7),  BIT(7),  "100baseTx" },
	{ BIT(6),  BIT(6),  "10baseT-FD" },
	{ BIT(5),  BIT(5),  "10baseT" },
	{ },
};

static void show_advert(u16 advert)
{
	show_mii_reg("I'm advertising", advert, c22_base_advert,
		     ".\n   Advertising ", " ");
	printf("   Advertising %sadditional info pages.\n",
		   advert & BIT(15) ? "" : "no ");
	show_protocol(advert & 31);
}

static void show_lpar(u16 lpar)
{
	show_mii_reg("Link partner advertisment is", lpar, c22_base_advert,
		     ".\n   Advertising ", " ");
	if (lpar & BIT(13))
		printf("   Remote fault.\n");
	printf("   Negotiation %s.\n",
		   lpar & BIT(14) ? "completed" : "did not complete");
}

static void show_xnp(const char *prefix, u16 adv)
{
	printf(" %s is %4.4x: ", prefix, adv);
	if (adv & BIT(13))
		printf("message page %u\n", adv & 0x07ff);
	else
		printf("unformatted code %3.3x\n", adv & 0x07ff);
}

static void show_xnp_advert(u16 adv)
{
	show_xnp("XNP advert", adv);
}

static void show_xnp_lpar(u16 lpa)
{
	show_xnp("XNP link partner", lpa);
}

// Clause 28 base page and 1000Base-T control/status
static void show_c28_advert(struct phy *phy, u16 *regs)
{
	show_advert(regs[4]);
	if (regs[1] & BIT(8)) {
		printf(" 1000baseT control %4.4x:", regs[9]);
		if (regs[9] & BIT(8))
			printf(" 1000baseT-HD");
		if (regs[9] & BIT(9))
			printf(" 1000baseT-FD");
		printf("\n");
	}
}

static void show_c28_lpar(struct phy *phy, u16 *regs)
{
	show_lpar(regs[5]);
	if (regs[1] & BIT(8)) {
		printf(" 1000baseT status %4.4x:", regs[10]);
		if (regs[10] & BIT(10))
			printf(" 1000baseT-HD");
		if (regs[10] & BIT(11))
			printf(" 1000baseT-FD");
		if (regs[10] & BIT(12))
			printf(" RemoteRxOK");
		if (regs[10] & BIT(13))
			printf(" LocalRxOK");
		printf("\n");
	}
}

static const struct c22_decoder c28_linkword = {
	.show_advert = show_c28_advert,
	.show_lpar = show_c28_lpar,
};

// Clause 37 1000baseX advert
static void show_c37_advert(struct phy *phy, u16 *regs)
{
	u16 base = regs[4];

	printf(" I'm advertising %4.4x:", base);
	if (base & BIT(6))
		printf(" 1000baseX");
	if (base & BIT(5))
		printf(" 1000baseX-FD");
	if (base & BIT(7))
		printf(" Pause");
	if (base & BIT(8))
		printf(" AsymPause");
	printf(".\n");
}

static void show_c37_lpar(struct phy *phy, u16 *regs)
{
	u16 base = regs[5];
	if (base) {
		printf(" Link partner advertisment is %4.4x.\n   Advertising", base);
		if (base & BIT(6))
			printf(" 1000baseX");
		if (base & BIT(5))
			printf(" 1000baseX-FD");
		if (base & BIT(7))
			printf(" Pause");
		if (base & BIT(8))
			printf(" AsymPause");
		printf(".\n");
	}
}

static const struct c22_decoder c37_linkword = {
	.show_advert = show_c37_advert,
	.show_lpar = show_c37_lpar,
};

// Cisco SGMII advert
static const struct bit_decoder cisco_sgmii_adv_decoder[] = {
	{ 0x8000, 0x0000, "Link Down" },
	{ 0x8000, 0x8000, "Link Up, " },
	{ 0x8c00, 0x8000, "10M" },
	{ 0x8c00, 0x8400, "100M" },
	{ 0x8c00, 0x8800, "1000M" },
	{ 0x8c00, 0x8c00, "?" },
	{ 0x9000, 0x8000, "/Half" },
	{ 0x9000, 0x9000, "/Full" },
	{ 0, 0, NULL },
};

static void cisco_sgmii_advert(struct phy *phy, u16 *regs)
{
	const char *strings[17], **p = strings;
	u16 base = regs[4];

	printf(" SGMII advertisement %4.4x", base);
	if (base & 1)
		p = bit_decoder(p, base, cisco_sgmii_adv_decoder);
	else
		*p++ = " Link down";
	if (base & 0x63fe)
		*p++ = ", reserved bits are set";
	*p = NULL;
	print_strings(strings, ": ", "");
}

static void cisco_sgmii_lpar(struct phy *phy, u16 *regs)
{
	u16 base = regs[5];

	printf(" SGMII acknowledgement %4.4x: ", base);
	if (base & 1)
		printf("%s.", base == 0x4001 ? "MAC acknowledged" : "unknown");
	else if (base == 0)
		printf("Unacknowledged.");

	printf("\n");
}

static const struct c22_decoder cisco_sgmii_linkword = {
	.show_advert = cisco_sgmii_advert,
	.show_lpar = cisco_sgmii_lpar,
};

// Clause 73.6 (baseR) advert
static const struct bit_decoder c73_linkword_high16[] = {
	{ BIT(15), BIT(15), "FEC requested"	},	/* F1 */
	{ BIT(14), BIT(14), "FEC ability"	},	/* F0 */
	{ },
};

static const struct bit_decoder c73_linkword_mid16[] = {
	{ BIT(13), BIT(13), "100GbaseCR4"	},	/* A8 */
	{ BIT(12), BIT(12), "100GbaseKR4"	},	/* A7 */
	{ BIT(11), BIT(11), "100GbaseKP4"	},	/* A6 */
	{ BIT(10), BIT(10), "100GbaseCR10"	},	/* A5 */
	{ BIT(9),  BIT(9),  "40GbaseCR4"	},	/* A4 */
	{ BIT(8),  BIT(8),  "40GbaseKR4"	},	/* A3 */
	{ BIT(7),  BIT(7),  "10GbaseKR"		},	/* A2 */
	{ BIT(6),  BIT(6),  "10GbaseKX4"	},	/* A1 */
	{ BIT(5),  BIT(5),  "1000baseKX"	},	/* A0 */
	{ },
};

static const struct bit_decoder c73_linkword_low16[] = {
	{ BIT(11), BIT(11), "AsmPause"		},	/* C1 */
	{ BIT(10), BIT(10), "Pause"		},	/* C0 */
	{ },
};

static const struct multi_bit_decoder c73_linkword[] = {
	{ 2, c73_linkword_high16 },		/* D32:D47 */
	{ 1, c73_linkword_mid16 },		/* D16:D31 */
	{ 0, c73_linkword_low16 },		/* D0:D15 */
	{ },
};

static void show_c73_linkword(const char *desc, u16 *lwp)
{
	printf(" %s %4.4x%4.4x%4.4x.\n", desc, lwp[2], lwp[1], lwp[0]);
	show_mii_multibits(lwp, c73_linkword, "   Advertising ", " ");
	if (lwp[0] & BIT(13))
		printf("   Remote fault.\n");
	show_protocol(lwp[0] & 31);
}


/* PMA/PMD tables */
// 1.0.5:2: Control register 1: Speed selection
static const char *pma_speed[16] = {
	[0x0] = "10Gb/s",
	[0x1] = "10PASS-TS/2base-TL",
	[0x2] = "40Gb/s",
	[0x3] = "100Gb/s",
	[0x4] = "25Gb/s",
	[0x6] = "2.5Gb/s",				// 802.3bz
	[0x7] = "5Gb/s",				// 802.3bz
};

// 1.4: PMA/PMD speed ability
static const struct bit_decoder mii_c45_spd_cap_pma[] = {
	{ BIT(8),  BIT(8),  "100G"	},
	{ BIT(7),  BIT(7),  "40G"	},
	{ BIT(0),  BIT(0),  "10G"	},
	{ BIT(6),  BIT(6),  "10G/1G"	},
	{ BIT(14), BIT(14), "5G"	},		// 802.3bz
	{ BIT(13), BIT(13), "2.5G"	},		// 802.3bz
	{ BIT(3),  BIT(3),  "1G"	},
	{ BIT(4),  BIT(4),  "100M"	},
	{ BIT(5),  BIT(5),  "10M"	},
	{ BIT(2),  BIT(2),  "10PASS-TS"	},
	{ BIT(1),  BIT(1),  "2base-TL"	},
	{ },
};

// 1.7: PMA/PMD control 2 register
static const char *mii_c45_type_pma[128] = {
	[0x00] = "10GbaseCX4",		[0x01] = "10GbaseEW",
	[0x02] = "10GbaseLW",		[0x03] = "10GbaseSW",
	[0x04] = "10GbaseLX4",		[0x05] = "10GbaseER",
	[0x06] = "10GbaseLR",		[0x07] = "10GbaseSR",
	[0x08] = "10GbaseLRM",		[0x09] = "10GbaseT",
	[0x0a] = "10GbaseKX4",		[0x0b] = "10GbaseKR",
	[0x0c] = "1000baseT",		[0x0d] = "1000baseKX",
	[0x0e] = "100baseTX",		[0x0f] = "10baseT",
	[0x10] = "10/1GbasePRX-D1",	[0x11] = "10/1GbasePRX-D2",
	[0x12] = "10/1GbasePRX-D3",	[0x13] = "10GbasePR-D1",
	[0x14] = "10GbasePR-D2",	[0x15] = "10GbasePR-D3",
	[0x16] = "10/1GbasePRX-U1",	[0x17] = "10/1GbasePRX-U2",
	[0x18] = "10/1GbasePRX-U3",	[0x19] = "10GbasePR-U1",
	[0x1a] = "10GbasePR-U3",
	[0x26] = "40GbaseT",					// 802.3bq
	[0x30] = "2.5GbaseT",		[0x31] = "5GbaseT",	// 802.3bz
	[0x37] = "25GbaseT",					// 802.3bq
	[0x40] = "40GbaseKR4",		[0x41] = "40GbaseCR4",
	[0x42] = "40GbaseSR4",		[0x43] = "40GbaseLR4",
	[0x44] = "40GbaseFR",
	[0x50] = "100GbaseCR10",	[0x51] = "100GbaseSR10",
	[0x52] = "100GbaseLR4",		[0x53] = "100GbaseER4",
};

// 1.8: PMA/PMD status 2 register
static const struct bit_decoder mii_c45_stat_2_pma[] = {
	{ BIT(0),  BIT(0),  "Local loopback"	},
	{ BIT(1),  BIT(1),  "10GbaseEW"		},
	{ BIT(2),  BIT(2),  "10GbaseLW"		},
	{ BIT(3),  BIT(3),  "10GbaseSW"		},
	{ BIT(4),  BIT(4),  "10GbaseLX4"	},
	{ BIT(5),  BIT(5),  "10GbaseER"		},
	{ BIT(6),  BIT(6),  "10GbaseLR"		},
	{ BIT(7),  BIT(7),  "10GbaseSR"		},
	{ BIT(8),  BIT(8),  "Transmit disable"	},
	{ BIT(12), BIT(12), "Receive fault"	},
	{ BIT(13), BIT(13), "Transmit fault"	},
	{ },
};

// 1.11: PMA/PMD extended ability register
static const struct bit_decoder mii_c45_ext_ability_pma[] = {
	{ BIT(10), BIT(10), "40G/100G (below)"	},
	{ BIT(0),  BIT(0),  "10GbaseCX4"	},
	{ BIT(1),  BIT(1),  "10GbaseLRM"	},
	{ BIT(2),  BIT(2),  "10GbaseT"		},
	{ BIT(3),  BIT(3),  "10GbaseKX4"	},
	{ BIT(4),  BIT(4),  "10GbaseKR"		},
	{ BIT(14), BIT(14), "2.5G/5G (below)"	},	// 802.3bz
	{ BIT(5),  BIT(5),  "1000baseT"		},
	{ BIT(6),  BIT(6),  "1000baseKX"	},
	{ BIT(7),  BIT(7),  "100baseTX"		},
	{ BIT(8),  BIT(8),  "10baseT"		},
	{ BIT(9),  BIT(9),  "P2MP"		},
	{ },
};

// 1.13: 40G/100G PMA/PMD extended ability register
static const struct bit_decoder mii_c45_40g_ext_abl_pma[] = {
	{ BIT(7),  BIT(7),  "100GbaseSR4"	},
	{ BIT(8),  BIT(8),  "100GbaseCR10"	},
	{ BIT(9),  BIT(9),  "100GbaseSR10"	},
	{ BIT(10), BIT(10), "100GbaseLR4"	},
	{ BIT(11), BIT(11), "100GbaseER4"	},
	{ BIT(12), BIT(12), "100GbaseKP4"	},
	{ BIT(13), BIT(13), "100GbaseKR4"	},
	{ BIT(14), BIT(14), "100GbaseCR4"	},
	{ BIT(0),  BIT(0),  "40GbaseKR4"	},
	{ BIT(1),  BIT(1),  "40GbaseCR4"	},
	{ BIT(2),  BIT(2),  "40GbaseSR4"	},
	{ BIT(3),  BIT(3),  "40GbaseLR4"	},
	{ BIT(4),  BIT(4),  "40GbaseFR"		},
	{ BIT(5),  BIT(5),  "40GbaseER4"	},
	{ BIT(6),  BIT(6),  "40GbaseT"		},	// 802.3bq
	{ BIT(15), BIT(15), "PMA remote loopback" },
	{ },
};

// 1.16: EEE capability
static const struct bit_decoder mii_c45_eee_capability_pma[] = {
	{ BIT(11), BIT(11), "100GBASE-CR4"	},
	{ BIT(10), BIT(10), "100GBASE-KR4"	},
	{ BIT(9),  BIT(9),  "100GBASE-KP4"	},
	{ BIT(8),  BIT(8),  "100GBASE-CR10"	},
	{ BIT(1),  BIT(1),  "40GBASE-CR4"	},
	{ BIT(0),  BIT(0),  "40GBASE-KR4"	},
	{ },
};

// 1.19: 25G PMA/PMD extended ability register
static const struct bit_decoder mii_c45_25g_ext_abl_pma[] = {
	{ BIT(5),  BIT(5),  "25GbaseT"		},	// 802.3bq
	{ },
};

// 1.21: 2.5G/5G PMA/PMD extended ability register
static const struct bit_decoder mii_c45_nbase_ext_abl_pma[] = {
	{ BIT(1),  BIT(1),  "5GbaseT"		},	// 802.3bz
	{ BIT(0),  BIT(0),  "2.5GbaseT"		},	// 802.3bz
	{ },
};


/* PCS */
// 3.0.5:2: Control register 1: Speed selection
static const char *pcs_speed[16] = {
	[0x0] = "10Gb/s",
	[0x1] = "10PASS-TS/2base-TL",
	[0x2] = "10/1Gb/s",
	[0x3] = "40Gb/s",
	[0x4] = "100Gb/s",
	[0x7] = "2.5Gb/s",				// 802.3bz
	[0x8] = "5Gb/s",				// 802.3bz
};

// 3.1: PCS status 1 register
static const struct bit_decoder mii_c45_pcs_stat[] = {
	{ BIT(11), BIT(11), "Tx LPI received"	},
	{ BIT(10), BIT(10), "Rx LPI received"	},
	{ BIT(9),  BIT(9),  "Tx LPI indication"	},
	{ BIT(8),  BIT(8),  "Rx LPI indication"	},
	{ BIT(6),  BIT(6),  "Clock stop capable" },
	{ BIT(1),  BIT(1),  "Supports low-power mode" },
	{ },
};

// 3.4: PCS speed ability register
static const struct bit_decoder mii_c45_spd_cap_pcs[] = {
	{ BIT(3),  BIT(3),  "100G"		},
	{ BIT(2),  BIT(2),  "40G"		},
	{ BIT(0),  BIT(0),  "10G"		},
	{ BIT(7),  BIT(7),  "5G"		},	// 802.3bz
	{ BIT(6),  BIT(6),  "2.5G"		},	// 802.3bz
	{ BIT(1),  BIT(1),  "10PASS-TS/2base-TL" },
	{ },
};

// 3.7.3:0 PCS type selection
static const char *mii_c45_type_pcs[16] = {
	[0x0] = "10GbaseR",
	[0x1] = "10GbaseX",
	[0x2] = "10GbaseW",
	[0x3] = "10GbaseT",
	[0x4] = "40GbaseR",
	[0x5] = "100GbaseR",
	[0x6] = "40GbaseT",				// 802.3bq
	[0x7] = "25GbaseR",				// 802.3bq
	[0x9] = "25GbaseT",				// 802.3bq
	[0xa] = "2.5GbaseT",				// 802.3bz
	[0xb] = "5GbaseT",				// 802.3bz
};

// 3.8: PCS status 2 register
static const struct bit_decoder mii_c45_stat_2_pcs[] = {
	{ BIT(5),  BIT(5),  "100GbaseR"		},
	{ BIT(4),  BIT(4),  "40GbaseR"		},
	{ BIT(6),  BIT(6),  "40GbaseT"		},	// 802.3bq
	{ BIT(9),  BIT(9),  "25GbaseT"		},	// 802.3bq
	{ BIT(0),  BIT(0),  "10GbaseR"		},
	{ BIT(1),  BIT(1),  "10GbaseX"		},
	{ BIT(2),  BIT(2),  "10GbaseW"		},
	{ BIT(3),  BIT(3),  "10GbaseT"		},
	{ BIT(12), BIT(12), "5GbaseT"		},	// 802.3bz
	{ BIT(13), BIT(13), "2.5GbaseT"		},	// 802.3bz
	{ },
};

// 3.20: EEE control and capability
static const struct bit_decoder mii_c45_eee_ctrl_capab_pcs[] = {
	{ BIT(13), BIT(13), "100GBASE-R deep sleep"	},
	{ BIT(12), BIT(12), "100GBASE-R fast wake"	},
	{ BIT(9),  BIT(9),  "40GBASE-R deep sleep"	},
	{ BIT(8),  BIT(8),  "40GBASE-R fast wake"	},
	{ BIT(6),  BIT(6),  "10GBASE-KR"		},
	{ BIT(5),  BIT(5),  "10GBASE-KX4"		},
	{ BIT(4),  BIT(4),  "1000BASE-KX"		},
	{ BIT(3),  BIT(3),  "10GBASE-T"			},
	{ BIT(2),  BIT(2),  "1000BASE-T"		},
	{ BIT(1),  BIT(1),  "100BASE-TX"		},
	{ BIT(0),  BIT(0),  "FastWake"			},
	{ },
};

// 3.32: baseR and MultiGbaseT PCS status 1 register
static const struct bit_decoder mii_pcs_basert_stat1[] = {
	{ BIT(12), BIT(12), "PCS receive link up"	},
	{ BIT(3),  BIT(3),  "PRBS9 pattern testing"	},
	{ BIT(2),  BIT(2),  "PRBS31 pattern testing"	},
	{ BIT(1),  BIT(1),  "High BER"			},
	{ BIT(0),  BIT(0),  "Block lock"		},
	{ },
};

// 3.33: baseR and MultiGbaseT PCS status 2 register
static const struct bit_decoder mii_pcs_basert_stat2[] = {
	{ BIT(15), BIT(15), "Block lock (latched)"	},
	{ BIT(14), BIT(14), "High BER (latched)"	},
	{ },
};

static const struct multi_bit_decoder mii_pcs_basert_stat[] = {
	{ MMD_PCS_BASERT_STAT1, mii_pcs_basert_stat1 },
	{ MMD_PCS_BASERT_STAT2, mii_pcs_basert_stat2 },
	{ },
};


/* PHY XGXS / DTE XGXS */
// 4.0.5:2: PHY XGXS Control register 1: Speed selection
// 5.0.5:2: DTE XGXS Control register 1: Speed selection
static const char *xs_speed[16] = {
	[0x0] = "10Gb/s",
};

// 4.1: PHYXS status 1 register
static const struct bit_decoder mii_c45_phyxs_stat[] = {
	{ BIT(11), BIT(11), "Tx LPI received"	},
	{ BIT(10), BIT(10), "Rx LPI received"	},
	{ BIT(9),  BIT(9),  "Tx LPI indication"	},
	{ BIT(8),  BIT(8),  "Rx LPI indication"	},
	{ BIT(6),  BIT(6),  "Clock stop capable" },
	{ BIT(1),  BIT(1),  "Supports low-power mode" },
	{ },
};

// 4.4: PHY XGXS speed ability register
// 5.4: DTE XGXS speed ability register
static const struct bit_decoder mii_c45_spd_cap_xs[] = {
	{ BIT(0),  BIT(0),  "10G" },
	{ },
};

// 4.20: EEE capability
// 5.20: EEE capability
static const struct bit_decoder mii_c45_eee_ctrl_capab_xs[] = {
	{ BIT(4),  BIT(4),  "EEE" },
	{ BIT(0),  BIT(0),  "XAUI stop capable" },
	{ },
};

// 4.24: PHY XGXS lane status register
static const struct bit_decoder mii_phyxs_lane_status[] = {
	{ BIT(12), BIT(12), "lanes synchronized and aligned" },
	{ BIT(11), BIT(11), "pattern testing supported" },
	{ BIT(10), BIT(10), "loopback support"		},
	{ BIT(3),  BIT(3),  "lane 3 synchronized"	},
	{ BIT(2),  BIT(2),  "lane 2 synchronized"	},
	{ BIT(1),  BIT(1),  "lane 2 synchronized"	},
	{ BIT(0),  BIT(0),  "lane 2 synchronized"	},
	{ },
};

/* AN */
// 7.1: AN status
static const struct bit_decoder mii_c45_an_stat[] = {
	{ BIT(3),  0, "Unable to perform Auto-negotiation.\n" },
	{ BIT(3) | BIT(5), BIT(3), "Able to perform Auto-negotiation, negotiation not complete.\n" },
	{ BIT(3) | BIT(5), BIT(3) | BIT(5), "Able to perform Auto-negotiation, negotiation complete.\n" },
	{ BIT(0),  BIT(0), "LP Auto-negotiation"	},
	{ BIT(4),  BIT(4), "Remote fault"		},
	{ BIT(6),  BIT(6), "Page Rcvd"			},
	{ BIT(7),  BIT(7), "Ext Next Pg"		},
	{ BIT(9),  BIT(9), "Parallel detection fault"	},
	{ },
};

// 7.32: 10GbaseT AN control register (tech bits)
static const struct bit_decoder mii_c45_an_multi_gbt_tech[] = {
	/* 0..2 below */
	{ BIT(3),  BIT(3),  "40GbaseT/fast"	},	// 802.3bq
	{ BIT(11), BIT(11), "40GbaseT"		},	// 802.3bq
	{ BIT(9),  BIT(9),  "25GbaseT/fast"	},	// 802.3bq
	{ BIT(10), BIT(10), "25GbaseT"		},	// 802.3bq
	{ BIT(12), BIT(12), "10GbaseT"		},
	{ BIT(6),  BIT(6),  "5GbaseT/fast"	},	// 802.3bz
	{ BIT(8),  BIT(8),  "5GbaseT"		},	// 802.3bz
	{ BIT(5),  BIT(5),  "2.5GbaseT/fast"	},	// 802.3bz
	{ BIT(7),  BIT(7),  "2.5GbaseT"		},	// 802.3bz
	/* 13..15 below */
	{ },
};

// 7.32: 10GbaseT AN control register (other bits)
static const struct bit_decoder mii_c45_an_multi_gbt_other[] = {
	{ 0, 0, "\n" },
	{ BIT(15) | BIT(14), BIT(15), "I'm configured to be the slave phy.\n" },
	{ BIT(15) | BIT(14), BIT(15) | BIT(14), "I'm configured to be the master phy.\n" },
	{ BIT(13), BIT(13), "I'm part of a multi-port device.\n" },
	{ BIT(13), 0,	    "I'm part of a single-port device.\n" },
	{ BIT(2),  BIT(2),  "10GbaseT LD frame reset" },
	{ BIT(1),  BIT(1),  "10GbaseT Fast retrain" },
	{ BIT(0),  BIT(0),  "10GbaseT LD loop timing" },
	{ },
};

static const struct multi_bit_decoder mii_c45_an_advert[] = {
	{ MMD_AN_MULTI_GBT_CTRL, mii_c45_an_multi_gbt_tech },
	{ MMD_AN_BASE_ADV0, c22_base_advert },
	{ MMD_AN_MULTI_GBT_CTRL, mii_c45_an_multi_gbt_other },
	{ },
};

// 7.33: 10GbaseT AN status register
static const struct bit_decoder mii_an_multi_gbt_stat[] = {
	{ BIT(15), BIT(15), "Master/slave config fault" },
	{ BIT(14), BIT(14), "Local PHY master"		},
	{ BIT(14), 0 << 14, "Local PHY slave"		},
	{ BIT(13), 0 << 13, "Local RX not ok"		},
	{ BIT(12), 0 << 13, "Remote RX not ok"		},
	{ BIT(10), BIT(10), "LP loop timing"		},
	{ BIT(8),  BIT(8),  "LP 40GbaseT"		},	// 802.3bq
	{ BIT(2),  BIT(2),  "LP 25GbaseT/fast"		},	// 802.3bq
	{ BIT(7),  BIT(7),  "LP 25GbaseT"		},	// 802.3bq
	{ BIT(9),  BIT(9),  "LP 10GbaseT PMA retrain req" },	// 802.3bq
	{ BIT(11), BIT(11), "LP 10GbaseT"		},
	{ BIT(4),  BIT(4),  "LP 5GbaseT/fast"		},	// 802.3bz
	{ BIT(6),  BIT(6),  "LP 5GbaseT"	        },	// 802.3bz
	{ BIT(3),  BIT(3),  "LP 2.5GbaseT/fast"		},	// 802.3bz
	{ BIT(5),  BIT(5),  "LP 2.5GbaseT"		},	// 802.3bz
	{ BIT(1),  BIT(1),  "LP Fast retrain"		},
	{ }
};

// 7.48: Backplane Ethernet, baseR copper status
#define BASE_R_PORT_TYPE_MASK	0x0f6e
static const struct bit_decoder mii_an_baser_status[] = {
	{ BASE_R_PORT_TYPE_MASK, BIT(1),  "1000baseKX"	},
	{ BASE_R_PORT_TYPE_MASK, BIT(2),  "10GbaseKX4 or CX4" },
	{ BASE_R_PORT_TYPE_MASK, BIT(3),  "10GbaseKR"	},
	{ BASE_R_PORT_TYPE_MASK, BIT(5),  "40GbaseKR4"	},
	{ BASE_R_PORT_TYPE_MASK, BIT(6),  "40GbaseCR4"	},
	{ BASE_R_PORT_TYPE_MASK, BIT(8),  "100GbaseCR10" },
	{ BASE_R_PORT_TYPE_MASK, BIT(9),  "100GbaseKP4"	},
	{ BASE_R_PORT_TYPE_MASK, BIT(10), "100GbaseKR4" },
	{ BASE_R_PORT_TYPE_MASK, BIT(11), "100GbaseCR4" },
	{ BIT(4), BIT(4), "FEC" },
	{ }
};

// 7.60: EEE advertisement
// 7.61: EEE link partner ability
static const struct bit_decoder mii_an_eee_advertisement[] = {
	{ BIT(13), BIT(13), "100GBASE-CR4"	},
	{ BIT(12), BIT(12), "100GBASE-KR4"	},
	{ BIT(11), BIT(11), "100GBASE-KP4"	},
	{ BIT(10), BIT(10), "100GBASE-CR10"	},
	{ BIT(8),  BIT(8),  "40GBASE-CR4"	},
	{ BIT(7),  BIT(7),  "40GBASE-KR4"	},
	{ BIT(6),  BIT(6),  "10GBASE-KR"	},
	{ BIT(5),  BIT(5),  "10GBASE-KX4"	},
	{ BIT(4),  BIT(4),  "1000BASE-KX"	},
	{ BIT(3),  BIT(3),  "10GBASE-T"		},
	{ BIT(2),  BIT(2),  "1000BASE-T"	},
	{ BIT(1),  BIT(1),  "100BASE-TX"	},
	{ }
};


static const char *mii_c45_speed(int devad)
{
	const char **speed_table = NULL;
	unsigned int speed;

	switch (devad) {
	case MDIO_MMD_PMAPMD:
		speed_table = pma_speed;
		break;
	case MDIO_MMD_PCS:
		speed_table = pcs_speed;
		break;
	case MDIO_MMD_PHY_XS:
	case MDIO_MMD_DTE_XS:
		speed_table = xs_speed;
		break;
	}
	switch (mii_val[MMD_CTRL1] & 0x2040) {
	case 0x0000:
		return "10Mb/s";
	case 0x0040:
		return "100Mb/s";
	case 0x2000:
		return "1Gb/s";
	case 0x2040:
		speed = (mii_val[MMD_CTRL1] & 0x003c) >> 2;
		if (speed_table && speed_table[speed])
			return speed_table[speed];
	default:
		return "Reserved";
	}
}

static void show_mii_c45_status2(const struct phy *phy,
	u16 stat2, const struct bit_decoder *decoder)
{
	u16 new_stat2 = mdio_read_phy(phy, MMD_STAT2);

	printf(" Status 2 register %4.4x ... %4.4x\n", stat2, new_stat2);

	if (decoder)
		show_mii_bits(stat2, decoder, "   Abilities: ", ", ");

	if (new_stat2 & BIT(11))
		printf("   *Transmit fault reported*.\n");
	else if (stat2 & BIT(11))
		printf("   Transmit fault previously reported, but now cleared.\n");
	if (new_stat2 & BIT(10))
		printf("   *Receive fault reported*.\n");
	else if (stat2 & BIT(10))
		printf("   Receive fault previously reported, but now cleared.\n");
}

static void show_mii_c45_pmapmd_10gbt_status(const struct phy *phy)
{
	static const char *mdi[4] = { "AB and CD crossed", "Reserved", "Reserved", "no crossover" };
	u16 rval[18];
	int val, i;

	for (i = 0; i < sizeof(rval) / 2; i++) {
		val = mdio_read_phy(phy, MMD_PMA_10GBT_SWAPPOL + i);
		rval[i] = val < 0 ? 0 : val;
	}

	printf(" Pair swap %4.4x: MDI %s, A %s, B %s, C %s, D %s.\n",
		rval[0],
		mdi[rval[0] & 3],
		rval[0] & BIT(8) ? "reversed" : "normal",
		rval[0] & BIT(9) ? "reversed" : "normal",
		rval[0] & BIT(10) ? "reversed" : "normal",
		rval[0] & BIT(11) ? "reversed" : "normal");
	if (rval[1])
		printf(" TX backoff %4.4x: LP TX backoff %udB TX backoff %udB%s.\n",
			rval[1],
			2 * ((rval[1] & 0xe000) >> 13),
			2 * ((rval[1] & 0x1c00) >> 10),
			rval[1] & 1 ? " short reach" : "");

#define SNR(x) (((x) - 0x8000) / 10.f)
	printf(" Operating SNR: A: %5.1fdB  B: %5.1fdB  C: %5.1fdB  D: %5.1fdB.\n",
		SNR(rval[3]), SNR(rval[4]), SNR(rval[5]), SNR(rval[6]));
	printf(" Minimum SNR  : A: %5.1fdB  B: %5.1fdB  C: %5.1fdB  D: %5.1fdB.\n",
		SNR(rval[7]), SNR(rval[8]), SNR(rval[9]), SNR(rval[10]));
	if (rval[11] || rval[12] || rval[13] || rval[14])
		printf(" RX power     : A: %5.1fdBm B: %5.1fdBm C: %5.1fdBm D: %5.1fdBm.\n",
			SNR(rval[11]), SNR(rval[12]), SNR(rval[13]), SNR(rval[14]));
#define SKEW(x) (((int)((x) & 0x7f) - 64) * 1.25f)
	printf(" Skew A>B %6.2fns A>C %6.2fns A>D %6.2fns\n",
		SKEW(rval[15] >> 8), SKEW(rval[16]), SKEW(rval[16] >> 8));
	if (rval[17])
		printf(" Fast retrain %4.4x.\n",
			rval[17]);
}

static const char *c45_mmd_name(unsigned int devad)
{
	const char *name = mmd_name[devad];

	return name ? name : "";
}

static void show_mii_c45_details(const struct phy *phy)
{
	const struct bit_decoder *decoder;
	const char *type;
	const char *col_end = "\033[m";
	const char *col_red = "\033[31m";
	const char *col_grn = "\033[32m";
	int new_stat, val;

	if (mii_val[MMD_CTRL1] == 0xffff) {
		printf("  No MII transceiver present!.\n");
		return;
	}

	if (phy->devad == MDIO_MMD_VENDOR1 ||
	    phy->devad == MDIO_MMD_VENDOR2)
		return;

	if ((mii_val[MMD_STAT2] & 0xc000) != 0x8000 &&
	    (phy->phy_id & 0xc000) != 0xc000 &&
	    phy->devad != MDIO_MMD_AN) {
		printf("  No device responding at this address!\n");
		return;
	}

	printf(" Control 1 register %4.4x: ", mii_val[MMD_CTRL1]);
	switch (phy->devad) {
	case MDIO_MMD_PMAPMD:
		if (c45_an_ctrl & BIT(12)) {
			printf("Speed determined by auto-negotiation.\n");
			break;
		}
		/*FALLTHROUGH*/
	default:
		printf("Speed %s.\n", mii_c45_speed(phy->devad));
		break;
	case MDIO_MMD_AN:
		printf("Auto-negotiation %sabled.\n",
		       mii_val[MMD_CTRL1] & BIT(12) ? "en" : "dis");
		break;
	}

	new_stat = mdio_read_phy(phy, MMD_STAT1);
	printf(" Status register %4.4x ... %4.4x.\n",
	       mii_val[MMD_STAT1], new_stat);
	switch (phy->devad) {
	case MDIO_MMD_PMAPMD:	/* has fault */
	case MDIO_MMD_PCS:	/* has fault */
	case MDIO_MMD_DTE_XS:	/* has fault */
		type = "Receive link";
		break;
	case MDIO_MMD_PHY_XS:	/* has fault */
		type = "Transmit link";
		break;
	case MDIO_MMD_WIS:	/* no fault */
	case MDIO_MMD_AN:	/* no fault */
	default:
		type = NULL;
		break;
	}
	if (!type) {
		printf("   Link status: %s%sestablished%s.\n",
		       /* transmit on #4 */
			new_stat & BIT(2) ? col_grn : col_red,
			mii_val[MMD_STAT1] & BIT(2) ? "" :
			  new_stat & BIT(2) ? "previously broken, but now re" :
			   "not ", col_end);
	} else {
		printf("   %s status: %s%sestablished%s.%s\n", type,
		       /* transmit on #4 */
			new_stat & BIT(2) ? col_grn : col_red,
			mii_val[MMD_STAT1] & BIT(2) ? "" :
			  new_stat & BIT(2) ? "previously broken, but now re" :
			   "not ", col_end,
			new_stat & BIT(7) ? "\n   *Fault condition detected*" :
			  mii_val[MMD_STAT1] & BIT(7) ? "\n   Fault condition previously reported, but now cleared" : "");
	}
	switch (phy->devad) {
	case MDIO_MMD_PCS:
		decoder = mii_c45_pcs_stat;
		break;
	case MDIO_MMD_PHY_XS:
		decoder = mii_c45_phyxs_stat;
		break;
	case MDIO_MMD_AN:
		decoder = mii_c45_an_stat;
		break;
	default:
		decoder = NULL;
		break;
	}
	if (decoder)
		show_mii_bits(new_stat, decoder, "   ", ", ");

	switch (phy->devad) {
	case MDIO_MMD_PMAPMD:
		decoder = mii_c45_spd_cap_pma;
		type = mii_c45_type_pma[mii_val[MMD_CTRL2] & 0x007f];
		if (c45_an_ctrl & BIT(12))
			type = "determined by auto-negotiation";
		break;
	case MDIO_MMD_PCS:
		decoder = mii_c45_spd_cap_pcs;
		type = mii_c45_type_pcs[mii_val[MMD_CTRL2] & 0x000f];
		break;
	case MDIO_MMD_PHY_XS:
	case MDIO_MMD_DTE_XS:
		decoder = mii_c45_spd_cap_xs;
		type = NULL;
		break;
	default:
		decoder = NULL;
		type = NULL;
		break;
	}
	if (decoder)
		show_mii_reg("Speed capability", mii_val[MMD_SPEED],
			     decoder, ": ", ", ");

	if (phy->devad == MDIO_MMD_PMAPMD || phy->devad == MDIO_MMD_PCS) {
		if (!type)
			type = "unknown";
		printf(" Control 2 register %4.4x: Type %s.\n",
		       mii_val[MMD_CTRL2], type);
	}

	switch (phy->devad) {
	case MDIO_MMD_PMAPMD:
		show_mii_c45_status2(phy, mii_val[MMD_STAT2],
				     mii_c45_stat_2_pma);

		if (mii_val[MMD_PMA_EXT_ABL])
			show_mii_reg("Extended Ability register",
				     mii_val[MMD_PMA_EXT_ABL],
				     mii_c45_ext_ability_pma,
				     "\n   Abilities: ", ", ");

		// Don't have the information for this yet...
		if (mii_val[MMD_PMA_25G_EXT_ABL])
			show_mii_reg("25G Extended Ability register",
				     mii_val[MMD_PMA_25G_EXT_ABL],
				     mii_c45_25g_ext_abl_pma,
				     "\n   Abilities: ", ", ");

		if (mii_val[MMD_PMA_EXT_ABL] & BIT(10))
			show_mii_reg("40G/100G Extended Ability register",
				     mii_val[MMD_PMA_40G100G_EXT_ABL],
				     mii_c45_40g_ext_abl_pma,
				     "\n   Abilities: ", ", ");

		// This should be predicated on a bit in MMD_PMA_EXT_ABL
		// but 88x3310 doesn't do this!
		if (mii_val[MMD_PMA_NBASE_EXT_ABL])
			show_mii_reg("2.5G/5G Extended Ability register",
				     mii_val[MMD_PMA_NBASE_EXT_ABL],
				     mii_c45_nbase_ext_abl_pma,
				     "\n   Abilities: ", ", ");

		val = mdio_read_phy(phy, MMD_PMA_10GBT_STATUS);
		if (val & 1)
			show_mii_c45_pmapmd_10gbt_status(phy);

		if (mii_val[MMD_PMA_EEE_CAPABILITY])
			show_mii_reg("EEE capabilities",
				     mii_val[MMD_PMA_EEE_CAPABILITY],
				     mii_c45_eee_capability_pma,
				     "\n   ", ", ");
		break;

	case MDIO_MMD_PCS:
		show_mii_c45_status2(phy, mii_val[MMD_STAT2],
				     mii_c45_stat_2_pcs);
		printf(" baseR or 10GbaseT status %4.4x %4.4x",
			mii_val[MMD_PCS_BASERT_STAT1],
			mii_val[MMD_PCS_BASERT_STAT2]);
		show_mii_multibits(mii_val, mii_pcs_basert_stat,
				   "\n   ", "\n   ");
		if (mii_val[MMD_PCS_EEE_CTRL_CAPAB]) {
			show_mii_reg("EEE control and capabilities",
				     mii_val[MMD_PCS_EEE_CTRL_CAPAB],
				     mii_c45_eee_ctrl_capab_pcs,
				     "\n   ", ", ");
			printf(" EEE wake error counter: %4.4x\n",
			       mii_val[MMD_PCS_EEE_WAKE_ERR]);
		}
		break;

	case MDIO_MMD_PHY_XS:
		if (mii_val[MMD_PHY_XS_EEE_CAPAB]) {
			show_mii_reg("EEE capabilities",
				     mii_val[MMD_PHY_XS_EEE_CAPAB],
				     mii_c45_eee_ctrl_capab_xs,
				     "\n   ", ", ");
			printf(" EEE wake error counter: %4.4x\n",
			       mii_val[MMD_PHY_XS_EEE_WAKE_ERR]);
		}
		val = mii_val[MMD_PHY_XS_LANE_STATUS];
		show_mii_reg("Lane Status", val, mii_phyxs_lane_status,
			     ":\n   ", "\n   ");
		break;

	case MDIO_MMD_DTE_XS:
		if (mii_val[MMD_DTE_XS_EEE_CAPAB]) {
			show_mii_reg("EEE capabilities",
				     mii_val[MMD_DTE_XS_EEE_CAPAB],
				     mii_c45_eee_ctrl_capab_xs,
				     "\n   ", ", ");
			printf(" EEE wake error counter: %4.4x\n",
			       mii_val[MMD_DTE_XS_EEE_WAKE_ERR]);
		}
		break;

	case MDIO_MMD_AN:
		val = mdio_read_phy(phy, MMD_AN_BASER_STAT);
		if (val < 0)
			val = 0;

		if (val & 1) {
			/* bits defined as per 73.6. */
			show_c73_linkword("I'm advertising",
					  mii_val + MMD_AN_BASE_ADV0);
			if (mii_val[MMD_AN_BASE_LPA2] |
			    mii_val[MMD_AN_BASE_LPA1] |
			    mii_val[MMD_AN_BASE_LPA0])
				show_c73_linkword("Link partner advertisment is",
					          mii_val + MMD_AN_BASE_LPA0);

			show_mii_reg("baseR Status", val, mii_an_baser_status,
				      ": ", " ");
		} else {
			/* bits defined as per 28.2.1.2. */
			printf(" I'm advertising %4.4x %4.4x.\n",
				mii_val[MMD_AN_BASE_ADV0],
				mii_val[MMD_AN_MULTI_GBT_CTRL]);
			show_mii_multibits(mii_val, mii_c45_an_advert,
					   "   ", " ");
			printf("   Advertising %sadditional info pages.\n",
				   mii_val[MMD_AN_BASE_ADV0] & BIT(15) ? "" : "no ");
			show_protocol(mii_val[MMD_AN_BASE_ADV0] & 31);
			show_lpar(mii_val[MMD_AN_BASE_LPA0]);
		}

		if (mii_val[MMD_AN_MULTI_GBT_CTRL])
			show_mii_reg("10GbaseT status",
				     mii_val[MMD_AN_MULTI_GBT_STAT],
				     mii_an_multi_gbt_stat, "\n   ", ", ");
		
		if (mii_val[MMD_CTRL1] & BIT(13)) {
			show_xnp_advert(mii_val[MMD_AN_XNP_ADV0]);
			show_xnp_lpar(mii_val[MMD_AN_XNP_LPA0]);
		}

		if (mii_val[MMD_AN_EEE_ADV]) {
			show_mii_reg("EEE advertisement",
				     mii_val[MMD_AN_EEE_ADV],
				     mii_an_eee_advertisement, "\n   ", ", ");
		}
		if (mii_val[MMD_AN_EEE_LPA]) {
			show_mii_reg("EEE link partner advertisement",
				     mii_val[MMD_AN_EEE_LPA],
				     mii_an_eee_advertisement, "\n   ", ", ");
		}
		break;
	}
}


static void show_mii_c22_details(struct phy *phy, const struct c22_decoder *dec)
{
	const char *col_end = "\033[m";
	const char *col_red = "\033[31m";
	const char *col_grn = "\033[32m";
	int i;
	u16 bmcr, bmsr, new_bmsr;

	if (mii_val[0] == 0xffff) {
		printf("  No MII transceiver present!.\n");
		return;
	}
	bmcr = mii_val[0];
	bmsr = mii_val[1];
	printf(" Basic mode control register %4.4x:", bmcr);
	if (bmcr & BIT(12))
		printf(" Auto-negotiation enabled.\n");
	else
		printf(" Auto-negotiation disabled!\n"
			   "   Speed fixed at 10%s mbps, %s-duplex.\n",
			   bmcr & BIT(6) ? "00" : bmcr & BIT(13) ? "0" : "",
			   bmcr & BIT(8) ? "full":"half");
	if (bmcr & BIT(15))
		printf("  Transceiver currently being reset!\n");
	if (bmcr & BIT(14))
		printf("  Transceiver in loopback mode!\n");
	if (bmcr & BIT(11))
		printf("  Transceiver powered down!\n");
	if (bmcr & BIT(10))
		printf("  Transceiver isolated from the MII!\n");
	if (bmcr & BIT(9))
		printf("  Restarted auto-negotiation in progress!\n");
	if (bmcr & BIT(7))
		printf("  Internal Collision-Test enabled!\n");
	
	new_bmsr = mdio_read_phy(phy, 1);
	printf(" Basic mode status register %4.4x ... %4.4x.\n",
		   bmsr, new_bmsr);
	if (bmsr & BIT(8))
		printf("   With extended status register %4.4x.\n",
			mii_val[15]);
	printf("   Link status: %s%sestablished%s.\n"
	       "   Capable of",
               new_bmsr & BIT(2) ? col_grn : col_red,
	       bmsr & BIT(2) ? "" :
	        (new_bmsr & BIT(2)) ? "previously broken, but now re" : "not ",
	       col_end);
	if (bmsr & 0xF800 || (bmsr & BIT(8) && mii_val[15] & 0xf000)) {
		for (i = 15; i >= 11; i--)
			if (bmsr & (1<<i))
				printf(" %s", media_names[i-11]);
		if (bmsr & BIT(8)) {
			if (mii_val[15] & BIT(12))
				printf(" 1000baseT");
			if (mii_val[15] & BIT(13))
				printf(" 1000baseT-FD");
			if (mii_val[15] & BIT(14))
				printf(" 1000baseX");
			if (mii_val[15] & BIT(15))
				printf(" 1000baseX-FD");
		}
	} else
		printf(" <Warning! No media capabilities>");

	printf(".\n"
		   "   %s to perform Auto-negotiation, negotiation %scomplete.\n",
		   bmsr & BIT(3) ? "Able" : "Unable",
		   bmsr & BIT(5) ? "" : "not ");

	if (bmsr & BIT(4))
		printf(" Remote fault detected!\n");
	if (bmsr & BIT(1))
		printf("   *** Link Jabber! ***\n");

	dec->show_advert(phy, mii_val);
	dec->show_lpar(phy, mii_val);
}

static int show_c22_subdev(struct phy *phy, int subdev, unsigned int off,
			   const struct c22_decoder *dec)
{
	int vendor = 0;

	phy->offset = off;

	if (phy->phy_id & 0x8000)
		printf("\n MII PHY #%d:%d %s",
		       (phy->phy_id >> 5) & 31, phy->devad,
		       c45_mmd_name(phy->devad));
	else
		printf("\n MII PHY #%d", phy->phy_id);

	if (subdev)
		printf(" Subdevice #%d", subdev);

	printf(" transceiver registers:");
	read_mii_regs(phy->ioaddr, phy->phy_id | phy->devad, off, 32);
	show_mii_regs(32);
	printf(".\n");
	if (mii_val[2] ^ mii_val[3]) { 	/* Eliminate 0x0000 and 0xffff IDs. */
		vendor = lookup_vendor(mii_val[2], mii_val[3]);
	} else {
		printf(" This transceiver has no vendor identification.\n");
	}
	show_mii_c22_details(phy, dec);

	return vendor;
}

static int show_c45_subdev(struct phy *phy, int subdev, unsigned int off)
{
	const char *name = c45_mmd_name(phy->devad);
	int vendor = 0;

	phy->offset = off;

	if (subdev)
		printf("\n MII PHY #%d:%d %s Subdevice #%d transceiver registers:",
		        (phy->phy_id >> 5) & 31, phy->devad, name, subdev);
	else
		printf("\n MII PHY #%d:%d %s transceiver registers:",
			(phy->phy_id >> 5) & 31, phy->devad, name);
	read_mii_regs(phy->ioaddr, phy->phy_id | phy->devad, off, 64);
	show_mii_regs(64);
	printf(".\n");

	if (phy->devad != MDIO_MMD_VENDOR1 && phy->devad != MDIO_MMD_VENDOR2) {
		if (mii_val[MMD_DEVID1] ^ mii_val[MMD_DEVID2]) {
			vendor = lookup_vendor(mii_val[MMD_DEVID1],
					       mii_val[MMD_DEVID2]);
		} else {
			printf(" This transceiver has no vendor identification.\n");
		}
	}

	show_mii_c45_details(phy);

	return vendor;
}


void show_mii_details(long ioaddr, int phy_id)
{
	DEFINE_C22_PHY(phy, ioaddr, phy_id);
	int vendor, i;

	/* This may not be omitted from the output. */
	printf("%s", version_msg);
	if ((phy_id & 0x801f) == 0x8000) {
		int val;
		u32 v, mmds = 0;
		for (i = 1; i < 32; i++) {
			val = mdio_read(ioaddr, phy_id | i, MMD_DEVPKG1);
			if (val < 0)
				continue;
			v = val;
			val = mdio_read(ioaddr, phy_id | i, MMD_DEVPKG2);
			if (val < 0)
				continue;
			v |= val << 16;
			if (v == 0xffffffff)
				continue;
			mmds = v;
			break;
		}

		if (mmds & BIT(7)) {
			val = mdio_read(ioaddr, phy_id | MDIO_MMD_AN, MMD_CTRL1);
			if (val >= 0)
				c45_an_ctrl = val;
		}

		for (i = 1; i < 32; i++) {
			if (mmds & (1 << i)) {
				phy.devad = i;
				vendor = show_c45_subdev(&phy, 0, 0);
				if (oui_map[vendor].func)
					oui_map[vendor].func(ioaddr, phy_id | i);
			}
		}
	} else if (phy_id & 0x8000) {
		phy.devad = phy.phy_id & 31;
		phy.phy_id &= ~31;

		vendor = show_c45_subdev(&phy, 0, 0);
		if (oui_map[vendor].func)
			oui_map[vendor].func(ioaddr, phy_id);
	} else {
		vendor = show_c22_subdev(&phy, 0, 0, &c28_linkword);
		if (oui_map[vendor].func)
			oui_map[vendor].func(ioaddr, phy_id);
	}
}

static void dump_mii_range(long ioaddr, int phy_id, unsigned int start,
			   unsigned int len)
{
	unsigned int i, j;
	int gap = 0;

	for (i = start; i < start + len; i += 8) {
		u16 vals[8], v;

		for (j = 0, v = 0; j < 8; j++) {
			int val = mdio_read(ioaddr, phy_id, i + j);
			if (val < 0)
				val = 0;
			vals[j] = val;
			v |= val;
		}

		if (v) {
			if (gap) {
				printf("\n  ...");
				gap = 0;
			}
			printf("\n  %4.4x:", i);
			for (j = 0; j < 8; j++)
				printf(" %4.4x", vals[j]);
		} else {
			gap = 1;
		}
	}
	printf("\n");
}

void dump_mii(long ioaddr, int phy_id)
{
	unsigned int max;

	if (phy_id & 0x8000)
		max = 65536;
	else
		max = 32;

	printf(" MII PHY #%d transceiver registers:", phy_id);
	dump_mii_range(ioaddr, phy_id, 0, max);
}

int monitor_mii(long ioaddr, int phy_id)
{
	int i, last_event = 0;
	unsigned short new_1, baseline_1 = mdio_read(ioaddr, phy_id, 1);
	struct timeval tv, sleepval;
	time_t cur_time;
	char timebuf[12];

	if (baseline_1 == 0xffff) {
		fprintf(stderr, "No MII transceiver present to monitor.\n");
		return -1;
	}

	gettimeofday(&tv, NULL);
	cur_time = tv.tv_sec;
	strftime(timebuf, sizeof(timebuf), "%H:%M:%S", localtime(&cur_time));

	printf("Monitoring the MII transceiver status.\n"
		   "%s.%03d  Baseline value of MII BMSR (basic mode status register)"
		   " is %4.4x.\n", timebuf, (int)tv.tv_usec/1000, baseline_1);
	while (1) {
		new_1 = mdio_read(ioaddr, phy_id, 1);
		if (new_1 == 0xffff) {
			fprintf(stderr, "The MII transceiver is no longer accessable!\n");
			return -1;
		}
		if (new_1 != baseline_1) {
			gettimeofday(&tv, NULL);
			cur_time = tv.tv_sec;
			strftime(timebuf, sizeof(timebuf), "%H:%M:%S",
					 localtime(&cur_time));
			printf("%s.%03d  MII BMSR now %4.4x: %4s link, NWay %s, "
				   "%3sJabber%s (%4.4x).\n",
				   timebuf, (int)tv.tv_usec/1000, new_1,
				   new_1 & 0x04 ? "Good" : "no",
				   new_1 & 0x20 ? "done" : "busy",
				   new_1 & 0x02 ? "" : "No ",
				   new_1 & 0x10 ? ", remote fault" : "",
				   mdio_read(ioaddr, phy_id, 5)
				   );
			if (!(baseline_1 & 0x20)  && (new_1 & 0x20)) {
				int lkpar = mdio_read(ioaddr, phy_id, 5);
				printf("   New link partner capability is %4.4x %4.4x:",
					   lkpar, mdio_read(ioaddr, phy_id, 6));
				switch (lkpar) {
				case 0x45e1: printf(" 10/100 switch w/ flow control"); break;
				case 0x41e1: printf(" 10/100 HD+FD switch"); break;
				case 0x40a1: printf(" 10/100 bridged repeater"); break;
				case 0x4081: printf(" 100baseTx repeater w/autonegotation");
					break;
				case 0x0081: printf(" 100baseTx (no autonegotation)"); break;
				case 0x4021: printf(" 10baseT repeater w/autonegotation");
					break;
				case 0x0021: printf(" 10baseT (no autonegotation)"); break;
				default:
					for (i = 9; i >= 5; i--)
						if (lkpar & (1<<i))
							printf(" %s", media_names[i-5]);
				}
				printf(".\n");
			}
			fflush(stdout);
			baseline_1 = new_1;
			last_event = 0;
		}
		sleepval.tv_sec = 0;
		sleepval.tv_usec = last_event++ > 30 ? 200000 : 1000;
		select(0, 0, 0, 0, &sleepval);			/* Or just sleep(1); */
	}
	printf("  Value of MII BMSR (basic mode status register) is %4.4x.\n",
		   mdio_read(ioaddr, phy_id, 1));
	return 0;
}

/* Emit transceiver-specific info. */

static void qs6612(long ioaddr, int phy_id)
{
	printf("  QS6612 extra registers: Mode %4.4x.\n"
		   "    Interrupt source %4.4x, mask %4.4x.\n"
		   "    PHY control %4.4x.\n",
		   mii_val[17], mii_val[29], mii_val[30], mii_val[31]);
	return;
}

static void ns83843(long ioaddr, int phy_id)
{
	printf("  NatSemi 83843 extra registers:\n"
		   "    PHY status %4.4x\n"
		   "    %s link, %d Mb/sec %s duplex\n"
		   "    MII interrupts %sabled, %s pending.\n"
		   "    Events since last read\n"
		   "     Link disconnects %d\n"
		   "     False carriers %d\n"
		   "     Receive errors %d\n"
		   "    Link beat is currently %sstable\n",
		   mii_val[0x10],
		   mii_val[10] & BIT(0) ? "Valid" : "Invalid",
		   mii_val[10] & BIT(1) ? 10 : 100, 
		   mii_val[10] & BIT(2) ? "full" : "half", 
		   mii_val[0x11] & BIT(1) ? "en":"dis",
		   mii_val[0x10] & BIT(8) ? "interrupt": "none",
		   mii_val[0x13], mii_val[0x14], mii_val[0x15],
		   mii_val[0x16] & BIT(4) ? "UN" : "");
	return;
}
static void smsc83c180(long ioaddr, int phy_id)
{
	int mii_reg25 = mii_val[25];
	printf("  SMSC 83c180 extra registers:\n"
		   "    Auto-negotiation status 0x%4.4x.\n"
		   "      10baseT polarity is %s.\n"
		   "      PHY address is %d.\n"
		   "      Auto-negotiation %scomplete, 1%s0Mbps %s duplex.\n"
		   "    Rx symbol errors since last read %d.\n",
		   mii_reg25,
		   mii_reg25 & BIT(13) ? "normal" : "reversed",
		   (mii_reg25>>8) & 0x1F,
		   mii_reg25 & BIT(7) ? "did not " : "",
		   mii_reg25 & BIT(5) ? "0" : "",
		   mii_reg25 & BIT(6) ? "full" : "half",
		   mdio_read(ioaddr, phy_id, 26));
	return;
}

static const char *tdk_events[8] = {
	"Jabber", "Rx error", "Negotiation page received", "Link detection fault",
	"Link partner acknowledge", "Link status change", "Remote partner fault",
	"Auto-Negotiation complete"};

static const struct msg_tbl tdk_reg16[] = {
	{0x8000, " Transceiver is in repeater mode!"},
	{0x4000, " Interrupt pin set to active high."},
	{0x2000, " Reserved bit 12 is unexpectedly set."},
	{0x1000, " Transmit pins are internally disconnected."},
	{0x0800, " 10baseT signal quality test is disabled."},
	{0x0400, " 10baseT loopback mode."},
	{0, 0},
};

static void tdk78q2120(long ioaddr, int phy_id)
{
	int mii_reg16 = mii_val[16];
	int mii_reg17 = mii_val[17];
	int mii_reg18 = mii_val[18];
	int i;
	printf("  TDK format vendor-specific registers 16..18 are "
		   "0x%4.4x 0x%4.4x 0x%4.4x\n", mii_reg16, mii_reg17, mii_reg18);
	printf("      Link polarity is %s %s.\n"
		   "%s%s"
		   "      Auto-negotiation %s, 1%s0Mbps %s duplex.\n"
		   "      Rx link in %s state, PLL %s.\n",
		   mii_reg16 & BIT(5) ? "OVERRIDDEN to" : "detected as",
		   mii_reg16 & BIT(4) ? "reversed" : "normal",
		   mii_reg16 & BIT(1) ?
		   "     100baseTx Coding and scrambling is disabled!\n":"",
		   mii_reg16 & BIT(0) ? "     Rx_CLK power-save mode is enabled!\n":"",
		   mii_reg18 & BIT(12) ? "had no common media" : "complete",
		   mii_reg18 & BIT(10) ? "0" : "",
		   mii_reg18 & BIT(11) ? "full" : "half",
		   mii_reg18 & BIT(9) ? "pass" : "fail",
		   mii_reg18 & BIT(8) ? "slipped since last read" : "locked");

	msg_if_set(mii_reg16, tdk_reg16);
	if (mii_reg17 & 0x00ff) {
		printf("      Events since last read:");
		for (i = 0; i < 8; i++)
			if (mii_reg17 & (1 << i))
				printf("  %s", tdk_events[i]);
	} else
		printf("      No new link status events.");

	if (mii_reg17 & 0xff00) {
		printf("\n      Events that will raise an interrupt:");
		for (i = 0; i < 8; i++)
			if (mii_reg17 & (0x100 << i))
				printf("  %s", tdk_events[i]);
	}
	printf("\n");
	return;
}

static void davicom_dm9101(long ioaddr, int phy_id)
{
	printf("  Davicom vendor specific registers: 0x%4.4x 0x%4.4x 0x%4.4x.\n",
		   mii_val[16], mii_val[17], mii_val[18]);
}
static void intel_i553(long ioaddr, int phy_id)
{
	printf("  This transceiver is 100baseT4 only!  Register 16 is %4.4x.\n",
		   mii_val[16]);
}
/* http://www.enablesemi.com/cgi-bin/byteserve/Products/Docs/3VCardBus.pdf */
static void enablesemi(long ioaddr, int phy_id)
{
	printf("  Isolated %d times, %d false carrier events, %d Rx errors.\n",
		   mii_val[18], mii_val[19], mii_val[21]);
	printf("  Cable polarity is %s, 100Mb PLL is %slocked.\n",
		   mii_val[28]&BIT(15) ? "reversed" : "normal",
		   mii_val[27]&BIT(13) ? "" : "un");
}
/* The amd79c901 contains both PNA and 10/100 management registers.
   http://www.amd.com/products/npd/techdocs/22304.pdf
*/
static void amd_pna(long ioaddr, int phy_id)
{
	printf("  HomePNA transceiver in %s speed, %s power mode.\n",
		   mii_val[16] & 4 ? "high" : "low",
		   mii_val[16] & 2 ? "high" : "low");
	printf("  HomePNA noise level %d, peak power %d..\n",
		   mii_val[25] >> 8, mii_val[25] & 0xff);
}
static void amd_tx(long ioaddr, int phy_id)
{
	int mii_reg25 = mii_val[25];
	printf("  AMD vendor specific registers: 0x%4.4x 0x%4.4x 0x%4.4x.\n",
		   mii_val[16], mii_val[17], mii_val[18]);
	printf("  The link is %s in 10%s %s duplex mode, autonegotiation state "
		   "has%s changed.\n",
		   mii_reg25 & 8 ? "up" : "down",
		   mii_reg25 & 1 ? "0baseTx" : "baseT",
		   mii_reg25 & 4 ? "full" : "half",
		   mii_reg25 & 2 ? "" : " not");
}

static const struct msg_tbl admtek_reg21[] = {
	{0x4000, " Link test diabled: Ignoring lack of 10baseT link beat."},
	{0x2000, " Link forced up."},
	{0x1000, " Tx jabber check disabled."},
	{0x0080, " Transmitting 'Far End Fault'!"},
	{0x0040, " Rx error count full."},
	{0x0008, " Remote loop back enabled."},
	{0, 0},
};

static void admtek(long ioaddr, int phy_id)
{

	printf("  ADMtek vendor specific registers information:\n"
		   "   Cable length is approximately %d meters.\n"
		   "   The receiver has lost lock %d times since last check and "
		   "had %d error events.\n",
		   ((mii_val[20] & 0x00f0) >> 4)*10,
		   mii_val[23], mii_val[23]);
	msg_if_set(mii_val[21], admtek_reg21);
	tdk78q2120(ioaddr, phy_id);
}

static void lu3x31(long ioaddr, int phy_id)
{
	printf("  Lucent vendor specific registers 17: 0x%4.4x"
		   " 29: 0x%4.4x 30: 0x%4.4x 31: 0x%4.4x.\n",
		   mii_val[17], mii_val[29], mii_val[30], mii_val[31]);
}

static const struct msg_tbl myson_reg16[] = {
	{0x0080, " Far end fault enabled."},
	{0x0040, " Transformer ratio 1.25:1."},
	{0x0020, " Polarity correction diabled."},
	{0x0010, " Link is forced up regardless of link beat."},
	{0x0004, " Bypass Jabber check."},
	{0x0001, " 100baseFx mode selected."},
	{0, 0},
};

static void myson981(long ioaddr, int phy_id)
{
	int i, mii_reg17 = mii_val[17];

	printf("  Myson mtd981 extra registers: %4.4x %4.4x %4.4x %4.4x.\n",
		   mii_val[16], mii_val[17], mii_val[18], mii_val[19]);
	msg_if_set(mii_val[16] & 0xC800, tdk_reg16);
	msg_if_set(mii_val[16], myson_reg16);

	if (mii_reg17 & 0x00ff) {
		printf("      Events since last read:");
		for (i = 0; i < 8; i++)
			if (mii_reg17 & (1 << i))
				printf("  %s", tdk_events[i]);
	} else
		printf("      No new link status events.");
	if (mii_reg17 & 0xff00) {
		printf("\n      Events that will raise an interrupt:");
		for (i = 0; i < 8; i++)
			if (mii_reg17 & (0x100 << i))
				printf("  %s", tdk_events[i]);
	}
	printf("\n");

	return;
}

/* These are much like the TDK events in reversed order. */
static const struct msg_tbl via_reg17[] = {
	{0x0001, "Auto-Negotiation complete"},
	{0x0002, "Remote fault detected"},
	{0x0004, "Link failure detected"},
	{0x0008, "Bad Start Stream detected"},
	{0x0010, "Parallel detection fault"},
	{0x0020, "Extended negotiation page received"},
	{0x0040, "5B/4B code error detected"},
	{0x0080, "Jabber detected"},
	{0, 0},
};

static void via_tahoe(long ioaddr, int phy_id)
{
	int mii_reg16 = mii_val[16];
	int mii_reg17 = mii_val[17];
	int mii_reg18 = mii_val[18];

	printf("  VIA Tahoe extended registers: 16 %4.4x  #17 %4.4x  #18 %4.4x.\n",
		   mii_reg16, mii_reg17, mii_reg18);
	msg_if_set_fmt(mii_reg17, via_reg17, "   %s\n");
	printf("   Link %s 10%s Mbps %s duplex\n",
		   mii_reg18 & BIT(13) ? "up" : "down",
		   mii_reg18 & BIT(10) ? "0" : "",
		   mii_reg18 & BIT(11) ? "full" : "half");
}

/* Information from
   http://www.via.com.tw/en/datasheet/DS6103110.pdf
*/
   
static void via_vt6103(long ioaddr, int phy_id)
{
	printf("  VIA vt6103 error counts since the last check:\n"
		   "   The link has failed %d times.\n"
		   "   The receiver has lost lock %d times.\n"
		   "   There have been %d false carrier/SQE error.\n",
		   mii_val[21], mii_val[22], mii_val[23]);
}

/* Information from
   http://www.via.com.tw/en/Networking/DS6105LOM100.pdf
*/

static void via_vt6105(long ioaddr, int phy_id)
{
	printf("  VIA vt6105 PHY status:\n"
		   "   Duplex %s  speed %s\n",
		   mii_val[20] & BIT(0) ? "full" : "half",
		   mii_val[20] & BIT(1) ? "100" : "10");
}

/* Information from
   http://www.via.com.tw/en/Networking/DS6105LOM100.pdf
*/
static void intel(long ioaddr, int phy_id)
{
	printf("  Intel 8255* PHY #%d extended management registers:\n"
		   "    Error counts, cleared when read:\n"
		   "     False carriers %d\n"
		   "     Link disconnects %d\n"
		   "     Receive errors %d\n"
		   "     Rx symbol errors %d.\n"
		   "     Rx 10Mbps Early End-Of-Frame errors %d.\n"
		   "     Rx 100Mbps Early End-Of-Frame errors %d.\n"
		   "     Tx jabber errors %d.\n",
		   mii_val[18],
		   mii_val[19], mii_val[20], mii_val[21], mii_val[22], mii_val[23],
		   mii_val[24], mii_val[25]);
}

static void mv88x3310_xs(long ioaddr, int phy_id)
{
	DEFINE_C45_PHY(phy, ioaddr, phy_id & ~31, phy_id & 31);

	if (phy.devad != 4)
		return;

	show_c45_subdev(&phy, 2, 0x1000);
	show_c22_subdev(&phy, 3, 0x2000, &cisco_sgmii_linkword);
}

static void mv88x3310(long ioaddr, int phy_id)
{
	DEFINE_C45_PHY(phy, ioaddr, phy_id & ~31, phy_id & 31);

	switch (phy.devad) {
	case MDIO_MMD_PCS:
		show_c45_subdev(&phy, 2, 0x1000);
		show_c22_subdev(&phy, 3, 0x2000, &c37_linkword);
		break;

	case MDIO_MMD_AN:
		show_c45_subdev(&phy, 2, 0x1000);
		show_c45_subdev(&phy, 3, 0x1800);
		show_c45_subdev(&phy, 4, 0x2000);

		printf("\n Other registers");
		dump_mii_range(ioaddr, phy_id, 0x8000, 32);
		dump_mii_range(ioaddr, phy_id, 0x9000, 16);
		dump_mii_range(ioaddr, phy_id, 0x9800, 16);
		dump_mii_range(ioaddr, phy_id, 0xa000, 64);
		break;
	}
}



static int marvell_read_paged(const struct phy *phy, unsigned int addr)
{
	u16 val, old_page;
	long ioaddr = phy->ioaddr;
	int phy_id = phy->phy_id;

	old_page = mdio_read(ioaddr, phy_id, 22);
	mdio_write(ioaddr, phy_id, 22, phy->page);
	val = mdio_read(ioaddr, phy_id, addr);
	mdio_write(ioaddr, phy_id, 22, old_page);

	return val;
}

static const struct bit_decoder mv88e1111_pss_decoder[] = {
	{ 0xc800, 0x8800, "1000Mbps" },
	{ 0xc800, 0x4800, "100Mbps" },
	{ 0xc800, 0x0800, "10Mbps" },
	{ 0x2800, 0x2800, "Full" },
	{ 0x2800, 0x0800, "Half" },
	{ 0x0400, 0x0400, "Link Up" },
	{ 0x0440, 0x0440, "MDIX" },
	{ 0x0440, 0x0400, "MDI" },
	{ 0x0420, 0x0420, "Downshift" },
	{ 0x0808, 0x0808, "TX Pause" },
	{ 0x0804, 0x0804, "RX Pause" },
	{ },
};

static const struct bit_decoder mv88e1111_epss_decoder[] = {
	{ 0x8000, 0x8000, "Fiber/copper auto-selection disabled" },
	{ 0x8000, 0x0000, "Fiber/copper auto-selection enabled" },
	{ 0x2000, 0x2000, "Fiber link selected" },
	{ 0x2000, 0x0000, "Copper link selected" },
	{ 0x1000, 0x1000, "AN bypass enabled" },
	{ 0x1000, 0x0000, "AN bypass disabled" },
	{ 0x0800, 0x0800, "AN is bypassed" },
	{ 0x0200, 0x0200, "Auto-medium register selection enabled" },
	{ 0x000f, 0x0000, "SGMII w/ clock w/ SGMII AN to copper" },
	{ 0x000f, 0x0003, "RGMII to fiber" },
	{ 0x000f, 0x0004, "SGMII w/o clock w/ SGMII AN to copper" },
	{ 0x000f, 0x0006, "RGMII to SGMII" },
	{ 0x000f, 0x0007, "GMII to fiber" },
	{ 0x000f, 0x0008, "1000baseX w/o clock w/ 1000baseX AN to copper" },
	{ 0x000f, 0x0009, "RTBI to copper" },
	{ 0x000f, 0x000b, "RGMII/Modified MII to copper" },
	{ 0x000f, 0x000c, "1000baseX w/o clock w/o 1000baseX AN to copper" },
	{ 0x000f, 0x000d, "TBI to copper" },
	{ 0x000f, 0x000e, "GMII to SGMII" },
	{ 0x000f, 0x000f, "GMII to copper" },
	{ },
};

static void mv88e1111(long ioaddr, int phy_id)
{
	DEFINE_C22_PHY(phy, ioaddr, phy_id);
	int i;

	show_mii_reg("PHY Specific Status", mii_val[17],
		     mv88e1111_pss_decoder, "\n   ", ", ");

	show_mii_reg("Extended PHY Specific Status", mii_val[27],
		     mv88e1111_epss_decoder, "   ", ".\n   ");

	phy.page = 1;
	phy.mdio_read = marvell_read_paged;
	for (i = 0; i < 32; i++)
		mii_val[i] = mdio_read_phy(&phy, i);
	printf("\n MII PHY #%d fiber registers:", phy_id);
	show_mii_regs(32);
	printf(".\n");

	switch (mii_val[27] & 15) {
	case 0:
	case 4: /* SGMII to copper */
		show_mii_c22_details(&phy, &cisco_sgmii_linkword);
		break;
	case 8:
	case 12:
		/* GMII mode */
		show_mii_c22_details(&phy, &c37_linkword);
		break;
	default:
		break;
	}
}

static const struct bit_decoder mv88e151x_gcr[] = {
	{ 0x0040, 0x0040, "Auto-media:" },
	{ 0x0070, 0x0040, "Link on first media" },
	{ 0x0070, 0x0050, "Copper preferred" },
	{ 0x0070, 0x0060, "Fiber preferred" },
	{ 0x0070, 0x0070, "Reserved" },
	{ 0x0007, 0x0000, "RGMII(sys) to Copper" },
	{ 0x0007, 0x0001, "SGMII(sys) to Copper" },
	{ 0x0007, 0x0002, "RGMII(sys) to 1000baseX" },
	{ 0x0007, 0x0003, "SGMII(sys) to 100baseFX" },
	{ 0x0007, 0x0004, "SGMII(sys) to SGMII(media)" },
	{ 0x0007, 0x0005, "Reserved" },
	{ 0x0007, 0x0006, "RGMII(sys) to Auto" },
	{ 0x0007, 0x0007, "RGMII(sys) to Auto" },
	{ },
};

static const struct bit_decoder marvell_sgmii_adv_decoder[] = {
	{ 0x8000, 0x0000, "Link Down" },
	{ 0x8000, 0x8000, "Link Up, " },
	{ 0x8c00, 0x8000, "10M/" },
	{ 0x8c00, 0x8400, "100M/" },
	{ 0x8c00, 0x8800, "1000M/" },
	{ 0x8c00, 0x8c00, "?/" },
	{ 0x9000, 0x8000, "Half, " },
	{ 0x9000, 0x9000, "Full, " },
	/* These are the Marvell additions */
	{ 0x8200, 0x8200, "TxPause, " },
	{ 0x8100, 0x8100, "RxPause, " },
	{ 0x8080, 0x8000, "Copper" },
	{ 0x8080, 0x8080, "Fiber" },
	{ },
};

static void marvell_sgmii_advert(struct phy *phy, u16 *regs)
{
	const char *strings[17], **p = strings;
	u16 base = regs[4];

	printf(" SGMII advertisement %4.4x", base);
	if (base & 1) {
		p = bit_decoder(p, base, marvell_sgmii_adv_decoder);
	} else {
		*p++ = " Link down";
		*p = NULL;
	}
	if (base & 0x607e) {
		*p++ = ", reserved bits are set";
		*p = NULL;
	}
	print_strings(strings, ": ", "");
}

static const struct c22_decoder marvell_system_sgmii_linkword = {
	.show_advert = marvell_sgmii_advert,
	.show_lpar = cisco_sgmii_lpar,
};

static const struct bit_decoder marvell_fscr2_decoder[] = {
	{ 0x4000, 0x4000, "1000baseX noise filter" },
	{ 0x0200, 0x0200, "100baseFX FEFI enabled" },
	{ 0x0040, 0x0040, "AN bypass enabled" },
	{ 0x0040, 0x0000, "AN bypass disabled" },
	{ 0x0020, 0x0020, "AN was bypassed" },
	{ 0x0008, 0x0008, "Fiber TX disabled" },
	{ },
};

static void mv88e151x(long ioaddr, int phy_id)
{
	DEFINE_C22_PHY(phy, ioaddr, phy_id);
	u16 gcr;
	int i;

	phy.page = 18;
	gcr = mdio_read_phy(&phy, 20);
	show_mii_reg("General Control Register 1", gcr, mv88e151x_gcr, ": ", ", ");

	phy.page = 1;
	phy.mdio_read = marvell_read_paged;
	for (i = 0; i < 32; i++)
		mii_val[i] = mdio_read_phy(&phy, i);
	printf("\n MII PHY #%d fiber registers:", phy_id);
	show_mii_regs(32);
	printf(".\n");

	switch (mii_val[16] & 3) {
	case 0: /* 100baseFX */
		break;
	case 1: /* 1000baseX */
		show_mii_c22_details(&phy, &c37_linkword);
		break;
	case 2: /* SGMII System mode */
		show_mii_c22_details(&phy, &marvell_system_sgmii_linkword);
		break;
	case 3: /* SGMII Media mode */
		show_mii_c22_details(&phy, &cisco_sgmii_linkword);
		break;
	}
	show_mii_reg("Fiber Specific Control Register 2", mii_val[26],
		     marvell_fscr2_decoder, ": ", ", ");
}


/*
 * Local variables:
 *  compile-command: "cc -O -Wall -c libmii.c"
 *  c-indent-level: 4
 *  c-basic-offset: 4
 *  tab-width: 4
 * End:
 */