fxmac_rs 0.4.8

FXMAC Ethernet driver in Rust for PhytiumPi (Phytium Pi) board, supporting DMA-based packet transmission and reception.
Documentation
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
//! Core FXMAC Ethernet controller functionality.
//!
//! This module provides the main data structures and functions for controlling
//! the FXMAC Ethernet MAC controller.

use alloc::boxed::Box;
use core::sync::atomic::Ordering;

use crate::{fxmac_const::*, fxmac_dma::*, fxmac_intr::*, fxmac_phy::*, utils::*};

/// Handler type for DMA send (TX) interrupts.
pub const FXMAC_HANDLER_DMASEND: u32 = 1;
/// Handler type for DMA receive (RX) interrupts.
pub const FXMAC_HANDLER_DMARECV: u32 = 2;
/// Handler type for error interrupts.
pub const FXMAC_HANDLER_ERROR: u32 = 3;
/// Handler type for link status change interrupts.
pub const FXMAC_HANDLER_LINKCHANGE: u32 = 4;
/// Handler type for TX descriptor queue restart.
pub const FXMAC_HANDLER_RESTART: u32 = 5;

/// Link status: down.
pub const FXMAC_LINKDOWN: u32 = 0;
/// Link status: up.
pub const FXMAC_LINKUP: u32 = 1;
/// Link status: negotiating.
pub const FXMAC_NEGOTIATING: u32 = 2;

/// FXMAC0 peripheral clock frequency in Hz.
pub const FXMAC0_PCLK: u32 = 50000000;
/// FXMAC0 hotplug IRQ number.
pub const FXMAC0_HOTPLUG_IRQ_NUM: u32 = 53 + 30;
/// Maximum number of hardware queues supported.
pub const FXMAC_QUEUE_MAX_NUM: u32 = 4;

/// Mask for upper 32 bits of 64-bit address.
pub const ULONG64_HI_MASK: u64 = 0xFFFFFFFF00000000;
/// Mask for lower 32 bits of 64-bit address.
pub const ULONG64_LO_MASK: u64 = !ULONG64_HI_MASK;

/// Component is initialized and ready.
pub const FT_COMPONENT_IS_READY: u32 = 0x11111111;
/// Component is started.
pub const FT_COMPONENT_IS_STARTED: u32 = 0x22222222;

/// Memory page size in bytes.
pub const PAGE_SIZE: usize = 4096;
/// Base address of FXMAC0 controller.
pub(crate) const FXMAC_IOBASE: u64 = 0x3200c000;

/// Main FXMAC Ethernet controller instance.
///
/// This structure holds all state information for an FXMAC controller instance,
/// including configuration, DMA queues, and runtime status.
///
/// # Thread Safety
///
/// This structure implements `Send` and `Sync` for use across threads, but
/// external synchronization is required for concurrent access to mutable state.
///
/// # Example
///
/// ```ignore
/// let hwaddr: [u8; 6] = [0x55, 0x44, 0x33, 0x22, 0x11, 0x00];
/// let fxmac: &'static mut FXmac = xmac_init(&hwaddr);
///
/// // Check link status
/// if fxmac.link_status == FXMAC_LINKUP {
///     println!("Network link is up!");
/// }
/// ```
pub struct FXmac {
    /// Hardware configuration settings.
    pub config: FXmacConfig,
    /// Device initialization state (FT_COMPONENT_IS_READY when initialized).
    pub is_ready: u32,
    /// Device running state (FT_COMPONENT_IS_STARTED when active).
    pub is_started: u32,
    /// Current link status (FXMAC_LINKUP, FXMAC_LINKDOWN, or FXMAC_NEGOTIATING).
    pub link_status: u32,
    /// Currently enabled MAC options.
    pub options: u32,
    /// Interrupt mask for enabled interrupts.
    pub mask: u32,
    /// Capability mask bits.
    pub caps: u32,
    /// Network buffer management (lwIP port compatibility).
    pub lwipport: FXmacLwipPort,
    /// Transmit buffer descriptor queue.
    pub tx_bd_queue: FXmacQueue,
    /// Receive buffer descriptor queue.
    pub rx_bd_queue: FXmacQueue,
    /// Hardware module identification number.
    pub moudle_id: u32,
    /// Maximum transmission unit size.
    pub max_mtu_size: u32,
    /// Maximum frame size including headers.
    pub max_frame_size: u32,
    /// PHY address on the MDIO bus.
    pub phy_address: u32,
    /// Receive buffer mask for speed settings.
    pub rxbuf_mask: u32,
}

// SAFETY: FXmac can be sent between threads as long as proper synchronization
// is used for concurrent access.
unsafe impl Send for FXmac {}
// SAFETY: FXmac can be shared between threads with external synchronization.
unsafe impl Sync for FXmac {}

/// Hardware configuration for the FXMAC controller.
///
/// This structure contains all hardware-level configuration parameters
/// required to initialize and operate the FXMAC Ethernet controller.
pub struct FXmacConfig {
    /// Instance identifier for multi-controller setups.
    pub instance_id: u32,
    /// Base address of the MAC controller registers.
    pub base_address: u64,
    /// Base address for extended mode configuration.
    pub extral_mode_base: u64,
    /// Base address for loopback configuration.
    pub extral_loopback_base: u64,
    /// PHY interface type (SGMII, RGMII, etc.).
    pub interface: FXmacPhyInterface,
    /// Link speed in Mbps (10, 100, 1000, etc.).
    pub speed: u32,
    /// Duplex mode: 1 for full-duplex, 0 for half-duplex.
    pub duplex: u32,
    /// Auto-negotiation enable: 1 to enable, 0 to disable.
    pub auto_neg: u32,
    /// Peripheral clock frequency in Hz.
    pub pclk_hz: u32,
    /// Maximum number of hardware queues.
    pub max_queue_num: u32,
    /// TX queue index (0 to FXMAC_QUEUE_MAX_NUM-1).
    pub tx_queue_id: u32,
    /// RX queue index (0 to FXMAC_QUEUE_MAX_NUM-1).
    pub rx_queue_id: u32,
    /// Hotplug IRQ number.
    pub hotplug_irq_num: u32,
    /// DMA burst length setting.
    pub dma_brust_length: u32,
    /// Default network configuration options.
    pub network_default_config: u32,
    /// IRQ numbers for each hardware queue.
    pub queue_irq_num: [u32; FXMAC_QUEUE_MAX_NUM as usize],
    /// Capability flags (e.g., tail pointer support).
    pub caps: u32,
    /// MAC address (6 bytes).
    pub mac: [u8; 6],
}

/// Hardware queue structure for TX/RX operations.
pub struct FXmacQueue {
    /// Queue identifier.
    pub queue_id: u32,
    /// Buffer descriptor ring for this queue.
    pub bdring: FXmacBdRing,
}

/// PHY interface mode definitions.
///
/// Specifies the physical layer interface type used for communication
/// between the MAC controller and the PHY chip.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FXmacPhyInterface {
    /// SGMII (Serial Gigabit Media Independent Interface).
    FXMAC_PHY_INTERFACE_MODE_SGMII = 0,
    /// RMII (Reduced Media Independent Interface).
    FXMAC_PHY_INTERFACE_MODE_RMII = 1,
    /// RGMII (Reduced Gigabit Media Independent Interface).
    FXMAC_PHY_INTERFACE_MODE_RGMII = 2,
    /// XGMII (10 Gigabit Media Independent Interface).
    FXMAC_PHY_INTERFACE_MODE_XGMII = 3,
    /// USXGMII (Universal Serial 10 Gigabit Media Independent Interface).
    FXMAC_PHY_INTERFACE_MODE_USXGMII = 4,
    /// 5GBASE-R interface mode.
    FXMAC_PHY_INTERFACE_MODE_5GBASER = 5,
    /// 2500BASE-X interface mode.
    FXMAC_PHY_INTERFACE_MODE_2500BASEX = 6,
}

/// Reads a memory-mapped register via a physical address.
///
/// The address is translated using the platform's [`KernelFunc::phys_to_virt`]
/// implementation before a volatile read is performed.
pub fn read_reg<T>(src: *const T) -> T {
    unsafe {
        core::ptr::read_volatile(ax_crate_interface::call_interface!(
            crate::KernelFunc::phys_to_virt(src as usize)
        ) as *const T)
    }
}

/// Writes a value to a memory-mapped register via a physical address.
///
/// The address is translated using the platform's [`KernelFunc::phys_to_virt`]
/// implementation before a volatile write is performed.
pub fn write_reg<T>(dst: *mut T, value: T) {
    unsafe {
        core::ptr::write_volatile(
            ax_crate_interface::call_interface!(crate::KernelFunc::phys_to_virt(dst as usize))
                as *mut T,
            value,
        );
    }
}

/// Initializes the FXMAC Ethernet controller.
///
/// This function performs complete hardware initialization of the FXMAC controller,
/// including:
/// - Hardware reset and configuration
/// - PHY initialization and link establishment
/// - DMA buffer descriptor ring setup
/// - Interrupt handler registration
/// - MAC address configuration
///
/// # Arguments
///
/// * `hwaddr` - A 6-byte MAC address to assign to the controller.
///
/// # Returns
///
/// A static mutable reference to the initialized [`FXmac`] instance.
///
/// # Panics
///
/// This function may panic if:
/// - PHY initialization fails
/// - DMA memory allocation fails
///
/// # Example
///
/// ```ignore
/// // Define the MAC address
/// let hwaddr: [u8; 6] = [0x55, 0x44, 0x33, 0x22, 0x11, 0x00];
///
/// // Initialize the controller
/// let fxmac = xmac_init(&hwaddr);
///
/// // The controller is now ready for packet transmission and reception
/// assert_eq!(fxmac.is_started, FT_COMPONENT_IS_STARTED);
/// ```
///
/// # Note
///
/// The returned reference has `'static` lifetime and is stored in a global
/// atomic pointer. Only one instance should be active at a time.
pub fn xmac_init(hwaddr: &[u8; 6]) -> &'static mut FXmac {
    // FXmacConfig mac_config:
    // mac_config.instance_id=0,
    // mac_config.base_address=0x3200c000,
    // mac_config.extral_mode_base=0x3200dc00,
    // mac_config.extral_loopback_base=0x3200dc04,
    // mac_config.interface=0,
    // mac_config.speed=100,
    // mac_config.duplex=1,
    // mac_config.auto_neg=0,
    // mac_config.pclk_hz=50000000,
    // mac_config.max_queue_num=4,
    // mac_config.tx_queue_id=0,
    // mac_config.rx_queue_id=0
    // mac_config.hotplug_irq_num=83,
    // mac_config.dma_brust_length=16,
    // mac_config.network_default_config=0x37f0,
    // mac_config.queue_irq_num[0]=87,
    // mac_config.caps=0
    let mut mac_config: FXmacConfig = FXmacConfig {
        instance_id: FXMAC0_ID,
        base_address: FXMAC0_BASE_ADDR as u64,
        extral_mode_base: FXMAC0_MODE_SEL_BASE_ADDR as u64,
        extral_loopback_base: FXMAC0_LOOPBACK_SEL_BASE_ADDR as u64,
        interface: FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_SGMII,
        speed: 100,
        duplex: 1,
        auto_neg: 0,
        pclk_hz: FXMAC0_PCLK,
        max_queue_num: 4, // .max_queue_num = 16
        tx_queue_id: 0,
        rx_queue_id: 0,
        hotplug_irq_num: FXMAC0_HOTPLUG_IRQ_NUM,
        dma_brust_length: 16,
        network_default_config: FXMAC_DEFAULT_OPTIONS,
        queue_irq_num: [
            FXMAC0_QUEUE0_IRQ_NUM,
            FXMAC0_QUEUE1_IRQ_NUM,
            FXMAC0_QUEUE2_IRQ_NUM,
            FXMAC0_QUEUE3_IRQ_NUM,
        ],
        caps: 0,
        mac: *hwaddr,
    };

    let mut xmac = FXmac {
        config: mac_config,
        is_ready: FT_COMPONENT_IS_READY,
        is_started: 0,
        link_status: FXMAC_LINKDOWN,
        options: 0,
        mask: 0,
        caps: 0,
        lwipport: FXmacLwipPort {
            buffer: FXmacNetifBuffer::default(),
            feature: FXMAC_LWIP_PORT_CONFIG_MULTICAST_ADDRESS_FILITER,
            hwaddr: *hwaddr,
            recv_flg: 0,
        },
        tx_bd_queue: FXmacQueue {
            queue_id: 0,
            bdring: FXmacBdRing::default(),
        },
        rx_bd_queue: FXmacQueue {
            queue_id: 0,
            bdring: FXmacBdRing::default(),
        },
        moudle_id: 0,
        max_mtu_size: 0,
        max_frame_size: 0,
        phy_address: 0,
        rxbuf_mask: 0,
    };

    // xmac_config: interface=FXMAC_PHY_INTERFACE_MODE_SGMII, autonegotiation=0, phy_speed=FXMAC_PHY_SPEED_100M, phy_duplex=FXMAC_PHY_FULL_DUPLEX
    // FXmacDmaReset, moudle_id=12, max_frame_size=1518, max_queue_num=4 (或16), dma_brust_length=16
    // network_default_config = 0x37f0, base_address=0x3200c000,FXMAC_RXBUF_HASH_MASK: GENMASK(30, 29)= 0x60000000 (0b 0110_0000_0000_0000_0000000000000000)

    // mii_interface = 1 = FXMAC_LWIP_PORT_INTERFACE_SGMII;

    // FXmacLwipPortInit():
    // step 1: initialize instance
    // step 2: depend on config set some options : JUMBO / IGMP
    // step 3: FXmacSelectClk
    // step 4: FXmacInitInterface
    // step 5: initialize phy
    // step 6: initialize dma
    // step 7: initialize interrupt
    // step 8: start mac

    let mut status: u32 = 0;

    // Reset the hardware and set default options
    // xmac.link_status = FXMAC_LINKDOWN;
    // xmac.is_ready = FT_COMPONENT_IS_READY;

    FXmacReset(&mut xmac);

    // irq_handler = (FXmacIrqHandler)FXmacIrqStubHandler;
    // interrupts bit mask
    xmac.mask = FXMAC_IXR_LINKCHANGE_MASK
        | FXMAC_IXR_TX_ERR_MASK
        | FXMAC_IXR_RX_ERR_MASK
        | FXMAC_IXR_RXCOMPL_MASK; // FXMAC_INTR_MASK // 这里打开收包中断,关闭发包中断

    if (xmac.config.caps & FXMAC_CAPS_TAILPTR) != 0 {
        FXmacSetOptions(&mut xmac, FXMAC_TAIL_PTR_OPTION, 0);
        xmac.mask &= !FXMAC_IXR_TXUSED_MASK;
    }

    // xmac.lwipport.feature = LWIP_PORT_MODE_MULTICAST_ADDRESS_FILITER;
    FxmacFeatureSetOptions(xmac.lwipport.feature, &mut xmac);

    status = FXmacSetMacAddress(&xmac.lwipport.hwaddr, 0);

    // mac_config.interface = FXMAC_PHY_INTERFACE_MODE_SGMII;

    if xmac.config.interface != FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_USXGMII {
        // initialize phy
        status = FXmacPhyInit(&mut xmac, XMAC_PHY_RESET_ENABLE);
        if status != 0 {
            warn!("FXmacPhyInit is error");
        }
    } else {
        info!("interface == FXMAC_PHY_INTERFACE_MODE_USXGMII");
    }

    FXmacSelectClk(&mut xmac);
    FXmacInitInterface(&mut xmac);

    // initialize dma
    let mut dmacrreg: u32 = read_reg((xmac.config.base_address + FXMAC_DMACR_OFFSET) as *const u32);
    dmacrreg &= !(FXMAC_DMACR_BLENGTH_MASK);
    dmacrreg |= FXMAC_DMACR_INCR16_AHB_AXI_BURST; /* Attempt to use bursts of up to 16. */
    write_reg(
        (xmac.config.base_address + FXMAC_DMACR_OFFSET) as *mut u32,
        dmacrreg,
    );

    FXmacInitDma(&mut xmac);

    // initialize interrupt
    // 网卡中断初始化设置
    FXmacSetupIsr(&mut xmac);

    // end of FXmacLwipPortInit()

    if (xmac.lwipport.feature & FXMAC_LWIP_PORT_CONFIG_UNICAST_ADDRESS_FILITER) != 0 {
        debug!("Set unicast hash table");
        FXmac_SetHash(&mut xmac, hwaddr);
    }

    // 注册了 lwip_port->ops:
    // ethernetif_link_detect()
    // ethernetif_input()
    // ethernetif_deinit()
    // ethernetif_start() -> FXmacLwipPortStart() -> FXmacStart()
    // ethernetif_debug()

    // ethernetif_start()
    // start mac
    FXmacStart(&mut xmac);

    // 开始发包的函数:FXmacLwipPortTx()->FXmacSgsend() -> FXmacSendHandler() -> FXmacProcessSentBds()
    // 触发中断函数:FXmacIntrHandler()
    // 收包handle: FXmacRecvIsrHandler()->FXmacRecvHandler

    // XMAC.store(Box::into_raw(Box::new(xmac)), Ordering::Relaxed);

    // Box::leak方法,它可以将一个变量从内存中泄漏, 将其变为'static生命周期,因此可以赋值给全局静态变量
    let xmac_ref = Box::leak(Box::new(xmac));
    XMAC.store(xmac_ref as *mut FXmac, Ordering::Relaxed);

    xmac_ref
}

/// Starts the Ethernet controller.
///
/// This enables TX/RX paths based on configured options, starts DMA channels,
/// and enables the device interrupt mask.
///
/// # Panics
///
/// Panics if the instance is not in the ready state.
pub fn FXmacStart(instance_p: &mut FXmac) {
    assert!(instance_p.is_ready == FT_COMPONENT_IS_READY);

    // clear any existed int status
    write_reg(
        (instance_p.config.base_address + FXMAC_ISR_OFFSET) as *mut u32,
        FXMAC_IXR_ALL_MASK,
    );

    // Enable transmitter if not already enabled
    if (instance_p.config.network_default_config & FXMAC_TRANSMITTER_ENABLE_OPTION) != 0 {
        let reg_val =
            read_reg((instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *const u32);
        if (reg_val & FXMAC_NWCTRL_TXEN_MASK) == 0 {
            write_reg(
                (instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *mut u32,
                reg_val | FXMAC_NWCTRL_TXEN_MASK,
            );
        }
    }

    // Enable receiver if not already enabled
    if (instance_p.config.network_default_config & FXMAC_RECEIVER_ENABLE_OPTION) != 0 {
        let reg_val =
            read_reg((instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *const u32);
        info!("Enable receiver, FXMAC_NWCTRL_OFFSET = {:#x}", reg_val);
        if (reg_val & FXMAC_NWCTRL_RXEN_MASK) == 0 {
            write_reg(
                (instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *mut u32,
                reg_val | FXMAC_NWCTRL_RXEN_MASK,
            );
        }
    }
    info!(
        "FXMAC_NWCTRL_OFFSET = {:#x}",
        read_reg((instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *const u32)
    );

    info!("Enable TX and RX by Mask={:#x}", instance_p.mask);

    // 使能网卡中断: Enable TX and RX interrupt
    // FXMAC_INT_ENABLE(instance_p, instance_p->mask);
    // Enable interrupts specified in 'Mask'. The corresponding interrupt for each bit set to 1 in 'Mask', will be enabled.
    write_reg(
        (instance_p.config.base_address + FXMAC_IER_OFFSET) as *mut u32,
        instance_p.mask & FXMAC_IXR_ALL_MASK,
    );

    // Mark as started
    instance_p.is_started = FT_COMPONENT_IS_STARTED;
}

/// Gracefully stops the Ethernet MAC.
///
/// This disables interrupts, stops DMA channels, and shuts down TX/RX paths.
///
/// # Panics
///
/// Panics if the instance is not in the ready state.
pub fn FXmacStop(instance_p: &mut FXmac) {
    assert!(instance_p.is_ready == FT_COMPONENT_IS_READY);
    // Disable all interrupts
    write_reg(
        (instance_p.config.base_address + FXMAC_IDR_OFFSET) as *mut u32,
        FXMAC_IXR_ALL_MASK,
    );

    // Disable the receiver & transmitter
    let mut reg_val: u32 =
        read_reg((instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *const u32);
    reg_val &= !FXMAC_NWCTRL_RXEN_MASK;
    reg_val &= !FXMAC_NWCTRL_TXEN_MASK;
    write_reg(
        (instance_p.config.base_address + FXMAC_NWCTRL_OFFSET) as *mut u32,
        reg_val,
    );

    // Mark as stopped
    instance_p.is_started = 0;
}

// Perform a graceful reset of the Ethernet MAC. Resets the DMA channels, the
// transmitter, and the receiver.
//
// Steps to reset
// - Stops transmit and receive channels
// - Stops DMA
// - Configure transmit and receive buffer size to default
// - Clear transmit and receive status register and counters
// - Clear all interrupt sources
// - Clear phy (if there is any previously detected) address
// - Clear MAC addresses (1-4) as well as Type IDs and hash value
//

fn FXmacReset(instance_p: &mut FXmac) {
    let mut mac_addr: [u8; 6] = [0; 6];

    // Stop the device and reset hardware
    FXmacStop(instance_p);

    // Module identification number
    // instance_p->moudle_id = 12
    instance_p.moudle_id = (read_reg((FXMAC_IOBASE + FXMAC_REVISION_REG_OFFSET) as *const u32)
        & FXMAC_IDENTIFICATION_MASK)
        >> 16;
    info!(
        "FXmacReset, Got Moudle IDENTIFICATION: {}",
        instance_p.moudle_id
    );

    instance_p.max_mtu_size = FXMAC_MTU;
    instance_p.max_frame_size = FXMAC_MAX_FRAME_SIZE;
    instance_p.config.max_queue_num = 16;
    instance_p.config.dma_brust_length = 16;
    instance_p.config.network_default_config = FXMAC_DEFAULT_OPTIONS;

    instance_p.config.pclk_hz = FXMAC0_PCLK; // 50000000

    let netctrl =
        (FXMAC_NWCTRL_STATCLR_MASK & !FXMAC_NWCTRL_LOOPBACK_LOCAL_MASK) | FXMAC_NWCTRL_MDEN_MASK;
    write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, netctrl);

    FXmacConfigureCaps(instance_p);

    // mdio clock division
    let mut w_reg: u32 = FXmacClkDivGet(instance_p);
    // DMA bus width, DMA位宽为128
    w_reg |= FXmacDmaWidth(instance_p.moudle_id);
    write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, w_reg);

    FXmacDmaReset(instance_p);

    // This register, when read provides details of the status of the receive path.
    write_reg(
        (FXMAC_IOBASE + FXMAC_RXSR_OFFSET) as *mut u32,
        FXMAC_SR_ALL_MASK,
    );

    // write 1 ro the relavant bit location disable that particular interrupt
    write_reg(
        (FXMAC_IOBASE + FXMAC_IDR_OFFSET) as *mut u32,
        FXMAC_IXR_ALL_MASK,
    );

    let reg_val: u32 = read_reg((FXMAC_IOBASE + FXMAC_ISR_OFFSET) as *const u32);
    write_reg((FXMAC_IOBASE + FXMAC_ISR_OFFSET) as *mut u32, reg_val);

    write_reg(
        (FXMAC_IOBASE + FXMAC_TXSR_OFFSET) as *mut u32,
        FXMAC_SR_ALL_MASK,
    );

    FXmacClearHash();

    // set default mac address
    for i in 0..4 {
        FXmacSetMacAddress(&mac_addr, i);
        FXmacGetMacAddress(&mut mac_addr, i);
        FXmacSetTypeIdCheck(0, i);
    }

    // clear all counters
    for i in 0..((FXMAC_LAST_OFFSET - FXMAC_OCTTXL_OFFSET) / 4) {
        read_reg((FXMAC_IOBASE + FXMAC_OCTTXL_OFFSET + (i * 4)) as *mut u32);
    }

    // Sync default options with hardware but leave receiver and
    // transmitter disabled. They get enabled with FXmacStart() if
    // FXMAC_TRANSMITTER_ENABLE_OPTION and FXMAC_RECEIVER_ENABLE_OPTION are set.
    let options = instance_p.config.network_default_config
        & !(FXMAC_TRANSMITTER_ENABLE_OPTION | FXMAC_RECEIVER_ENABLE_OPTION);
    FXmacSetOptions(instance_p, options, 0);
    let options = !instance_p.config.network_default_config;
    FXmacClearOptions(instance_p, options, 0);
}

fn FXmacDmaReset(instance_p: &mut FXmac) {
    let max_frame_size: u32 = instance_p.max_frame_size;

    let mut dmacfg: u32 = 0;
    // let max_queue_num = 16;
    // let dma_brust_length = 16;

    let mut rx_buf_size: u32 = max_frame_size / FXMAC_RX_BUF_UNIT;
    rx_buf_size += if (max_frame_size % FXMAC_RX_BUF_UNIT) != 0 {
        1
    } else {
        0
    }; /* roundup */

    // moudle_id=12
    if (instance_p.moudle_id >= 2) {
        for queue in 0..instance_p.config.max_queue_num {
            dmacfg = 0;

            // 设置发包/收包 buffer队列的基地址
            FXmacSetQueuePtr(0, queue as u8, FXMAC_SEND);
            FXmacSetQueuePtr(0, queue as u8, FXMAC_RECV);

            if queue != 0 {
                write_reg(
                    (FXMAC_IOBASE + FXMAC_RXBUFQX_SIZE_OFFSET(queue as u64)) as *mut u32,
                    rx_buf_size,
                );
            } else
            // queue is 0
            {
                dmacfg |= (FXMAC_DMACR_RXBUF_MASK & (rx_buf_size << FXMAC_DMACR_RXBUF_SHIFT));
            }
        }

        dmacfg |= (instance_p.config.dma_brust_length & FXMAC_DMACR_BLENGTH_MASK);

        dmacfg &= !FXMAC_DMACR_ENDIAN_MASK;
        dmacfg &= !FXMAC_DMACR_SWAP_MANAGEMENT_MASK; /* 选择小端 */

        dmacfg &= !FXMAC_DMACR_TCPCKSUM_MASK; /* close  transmitter checksum generation engine */

        dmacfg &= !FXMAC_DMACR_ADDR_WIDTH_64;
        dmacfg |= FXMAC_DMACR_RXSIZE_MASK | FXMAC_DMACR_TXSIZE_MASK;
        // set this bit can enable auto discard rx frame when lack of receive source,
        // which avoid endless rx buffer not available error intrrupts.
        dmacfg |= FXMAC_DMACR_ORCE_DISCARD_ON_ERR_MASK; /* force_discard_on_rx_err */

        dmacfg |= FXMAC_DMACR_ADDR_WIDTH_64; // Just for aarch64
    } else {
        FXmacSetQueuePtr(0, 0, FXMAC_SEND);
        FXmacSetQueuePtr(0, 0, FXMAC_RECV);
        dmacfg |= (FXMAC_DMACR_RXBUF_MASK & (rx_buf_size << FXMAC_DMACR_RXBUF_SHIFT));
        dmacfg |= (instance_p.config.dma_brust_length & FXMAC_DMACR_BLENGTH_MASK);

        dmacfg &= !FXMAC_DMACR_ENDIAN_MASK;
        dmacfg &= !FXMAC_DMACR_SWAP_MANAGEMENT_MASK; /* 选择小端 */

        dmacfg &= !FXMAC_DMACR_TCPCKSUM_MASK; /* close  transmitter checksum generation engine */

        dmacfg &= !FXMAC_DMACR_ADDR_WIDTH_64;
        dmacfg |= FXMAC_DMACR_RXSIZE_MASK | FXMAC_DMACR_TXSIZE_MASK;
        // set this bit can enable auto discard rx frame when lack of receive source,
        // which avoid endless rx buffer not available error intrrupts.
        dmacfg |= FXMAC_DMACR_ORCE_DISCARD_ON_ERR_MASK; /* force_discard_on_rx_err */
        dmacfg |= FXMAC_DMACR_ADDR_WIDTH_64; // Just for aarch64
    }

    write_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *mut u32, dmacfg);
}

fn FXmacDmaWidth(moudle_id: u32) -> u32 {
    if moudle_id < 2 {
        return FXMAC_NWCFG_BUS_WIDTH_32_MASK;
    }

    let read_regs = read_reg((FXMAC_IOBASE + FXMAC_DESIGNCFG_DEBUG1_OFFSET) as *const u32);
    match ((read_regs & FXMAC_DESIGNCFG_DEBUG1_BUS_WIDTH_MASK) >> 25) {
        4 => {
            info!("bus width is 128");
            FXMAC_NWCFG_BUS_WIDTH_128_MASK
        }
        2 => {
            info!("bus width is 64");
            FXMAC_NWCFG_BUS_WIDTH_64_MASK
        }
        _ => {
            info!("bus width is 32");
            FXMAC_NWCFG_BUS_WIDTH_32_MASK
        }
    }
}

fn FxmacFeatureSetOptions(feature: u32, xmac_p: &mut FXmac) {
    let mut options: u32 = 0;

    if (feature & FXMAC_LWIP_PORT_CONFIG_JUMBO) != 0 {
        info!("FXMAC_JUMBO_ENABLE_OPTION is ok");
        options |= FXMAC_JUMBO_ENABLE_OPTION;
    }

    if (feature & FXMAC_LWIP_PORT_CONFIG_UNICAST_ADDRESS_FILITER) != 0 {
        info!("FXMAC_UNICAST_OPTION is ok");
        options |= FXMAC_UNICAST_OPTION;
    }

    if (feature & FXMAC_LWIP_PORT_CONFIG_MULTICAST_ADDRESS_FILITER) != 0 {
        info!("FXMAC_MULTICAST_OPTION is ok");
        options |= FXMAC_MULTICAST_OPTION;
    }
    // enable copy all frames
    if (feature & FXMAC_LWIP_PORT_CONFIG_COPY_ALL_FRAMES) != 0 {
        info!("FXMAC_PROMISC_OPTION is ok");
        options |= FXMAC_PROMISC_OPTION;
    }
    // close fcs check
    if (feature & FXMAC_LWIP_PORT_CONFIG_CLOSE_FCS_CHECK) != 0 {
        info!("FXMAC_FCS_STRIP_OPTION is ok");
        options |= FXMAC_FCS_STRIP_OPTION;
    }

    FXmacSetOptions(xmac_p, options, 0);
}

/// Sets the start address of the transmit/receive buffer queue.
///
/// # Arguments
///
/// * `queue_p` - Physical base address of the queue ring.
/// * `queue_num` - Queue index to configure.
/// * `direction` - [`FXMAC_SEND`] or [`FXMAC_RECV`].
///
/// # Note
///
/// The buffer queue address must be configured before calling [`FXmacStart`].
pub fn FXmacSetQueuePtr(queue_p: u64, queue_num: u8, direction: u32) {
    // assert!(instance_p.is_ready == FT_COMPONENT_IS_READY);
    // If already started, then just return

    let flag_queue_p = if queue_p == 0 { 1 } else { 0 };
    let FXMAC_QUEUE_REGISTER_OFFSET =
        |base_addr: u64, queue_id: u64| (base_addr + (queue_id - 1) * 4);

    if queue_num == 0 {
        if direction == FXMAC_SEND {
            // set base start address of TX buffer queue (tx buffer descriptor list)
            write_reg(
                (FXMAC_IOBASE + FXMAC_TXQBASE_OFFSET) as *mut u32,
                ((queue_p & ULONG64_LO_MASK) | flag_queue_p) as u32,
            );
        } else {
            // set base start address of RX buffer queue (rx buffer descriptor list)
            write_reg(
                (FXMAC_IOBASE + FXMAC_RXQBASE_OFFSET) as *mut u32,
                ((queue_p & ULONG64_LO_MASK) | flag_queue_p) as u32,
            );
        }
    } else if direction == FXMAC_SEND {
        write_reg(
            (FXMAC_IOBASE + FXMAC_QUEUE_REGISTER_OFFSET(FXMAC_TXQ1BASE_OFFSET, queue_num as u64))
                as *mut u32,
            ((queue_p & ULONG64_LO_MASK) | flag_queue_p) as u32,
        );
    } else {
        write_reg(
            (FXMAC_IOBASE + FXMAC_QUEUE_REGISTER_OFFSET(FXMAC_RXQ1BASE_OFFSET, queue_num as u64))
                as *mut u32,
            ((queue_p & ULONG64_LO_MASK) | flag_queue_p) as u32,
        );
    }

    if direction == FXMAC_SEND
    // Only for aarch64
    {
        // Set the MSB of TX Queue start address
        write_reg(
            (FXMAC_IOBASE + FXMAC_MSBBUF_TXQBASE_OFFSET) as *mut u32,
            ((queue_p & ULONG64_HI_MASK) >> 32) as u32,
        );
    } else {
        // Set the MSB of RX Queue start address
        write_reg(
            (FXMAC_IOBASE + FXMAC_MSBBUF_RXQBASE_OFFSET) as *mut u32,
            ((queue_p & ULONG64_HI_MASK) >> 32) as u32,
        );
    }
}

fn FXmacConfigureCaps(instance_p: &mut FXmac) {
    instance_p.caps = 0;
    let read_regs = read_reg((FXMAC_IOBASE + FXMAC_DESIGNCFG_DEBUG1_OFFSET) as *const u32);
    if (read_regs & FXMAC_DESIGNCFG_DEBUG1_BUS_IRQCOR_MASK) == 0 {
        instance_p.caps |= FXMAC_CAPS_ISR_CLEAR_ON_WRITE;
        info!(
            "Design ConfigReg1: {:#x} Has FXMAC_CAPS_ISR_CLEAR_ON_WRITE feature",
            read_regs
        );
    }
}

fn FXmacClkDivGet(instance_p: &mut FXmac) -> u32 {
    // moudle_id=12
    // let pclk_hz = 50000000;
    let pclk_hz = instance_p.config.pclk_hz; // FXMAC0_PCLK;

    if (pclk_hz <= 20000000) {
        FXMAC_NWCFG_CLOCK_DIV8_MASK
    } else if (pclk_hz <= 40000000) {
        FXMAC_NWCFG_CLOCK_DIV16_MASK
    } else if (pclk_hz <= 80000000) {
        FXMAC_NWCFG_CLOCK_DIV32_MASK
    } else if (instance_p.moudle_id >= 2) {
        if (pclk_hz <= 120000000) {
            FXMAC_NWCFG_CLOCK_DIV48_MASK
        } else if (pclk_hz <= 160000000) {
            FXMAC_NWCFG_CLOCK_DIV64_MASK
        } else if (pclk_hz <= 240000000) {
            FXMAC_NWCFG_CLOCK_DIV96_MASK
        } else if (pclk_hz <= 320000000) {
            FXMAC_NWCFG_CLOCK_DIV128_MASK
        } else {
            FXMAC_NWCFG_CLOCK_DIV224_MASK
        }
    } else {
        FXMAC_NWCFG_CLOCK_DIV64_MASK
    }
}

/// Set options for the driver/device. The driver should be stopped with
/// FXmacStop() before changing options.
fn FXmacSetOptions(instance_p: &mut FXmac, options: u32, queue_num: u32) -> u32 {
    let mut reg: u32 = 0; /* Generic register contents */
    let mut reg_netcfg: u32 = 0; /* Reflects original contents of NET_CONFIG */
    let mut reg_new_netcfg: u32 = 0; /* Reflects new contents of NET_CONFIG */
    let mut status: u32 = 0;

    // let is_started = 0;

    info!(
        "FXmacSetOptions, is_started={}, options={}, queue_num={}, max_queue_num={}",
        instance_p.is_started, options, queue_num, instance_p.config.max_queue_num
    );

    // Be sure device has been stopped
    if instance_p.is_started == FT_COMPONENT_IS_STARTED {
        status = 9; //FXMAC_ERR_MAC_IS_PROCESSING;
        error!("FXMAC is processing when calling FXmacSetOptions function");
    } else {
        // Many of these options will change the NET_CONFIG registers.
        // To reduce the amount of IO to the device, group these options here
        // and change them all at once.

        // Grab current register contents
        reg_netcfg = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);

        reg_new_netcfg = reg_netcfg;

        // It is configured to max 1536.
        if (options & FXMAC_FRAME1536_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_1536RXEN_MASK;
        }

        // Turn on VLAN packet only, only VLAN tagged will be accepted
        if (options & FXMAC_VLAN_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_NVLANDISC_MASK;
        }

        // Turn on FCS stripping on receive packets
        if (options & FXMAC_FCS_STRIP_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_FCS_REMOVE_MASK;
        }

        // Turn on length/type field checking on receive packets
        if (options & FXMAC_LENTYPE_ERR_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_LENGTH_FIELD_ERROR_FRAME_DISCARD_MASK;
        }

        // Turn on flow control
        if (options & FXMAC_FLOW_CONTROL_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_PAUSE_ENABLE_MASK;
        }

        // Turn on promiscuous frame filtering (all frames are received)
        if (options & FXMAC_PROMISC_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_COPYALLEN_MASK;
        }

        // Allow broadcast address reception
        if (options & FXMAC_BROADCAST_OPTION) != 0 {
            reg_new_netcfg &= !FXMAC_NWCFG_BCASTDI_MASK;
        }

        // Allow multicast address filtering
        if (options & FXMAC_MULTICAST_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_MCASTHASHEN_MASK;
        }

        if (options & FXMAC_UNICAST_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_UCASTHASHEN_MASK;
        }

        if (options & FXMAC_TAIL_PTR_OPTION) != 0 {
            write_reg((FXMAC_IOBASE + FXMAC_TAIL_ENABLE) as *mut u32, 0x80000001);
        }

        // enable RX checksum offload
        if (options & FXMAC_RX_CHKSUM_ENABLE_OPTION) != 0 {
            reg_new_netcfg |= FXMAC_NWCFG_RXCHKSUMEN_MASK;
        }

        // Enable jumbo frames
        if (options & FXMAC_JUMBO_ENABLE_OPTION) != 0 {
            instance_p.max_mtu_size = FXMAC_MTU_JUMBO;
            instance_p.max_frame_size = FXMAC_MAX_FRAME_SIZE_JUMBO;

            reg_new_netcfg |= FXMAC_NWCFG_JUMBO_MASK;

            write_reg(
                (FXMAC_IOBASE + FXMAC_JUMBOMAXLEN_OFFSET) as *mut u32,
                FXMAC_MAX_FRAME_SIZE_JUMBO,
            );

            write_reg(
                (FXMAC_IOBASE + FXMAC_TXQSEGALLOC_QLOWER_OFFSET) as *mut u32,
                FXMAC_TXQSEGALLOC_QLOWER_JUMBO_MASK,
            );

            if queue_num == 0 {
                let mut rx_buf_size: u32 = 0;
                reg = read_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *const u32);

                reg &= !FXMAC_DMACR_RXBUF_MASK;

                rx_buf_size = instance_p.max_frame_size / FXMAC_RX_BUF_UNIT;
                rx_buf_size += if (instance_p.max_frame_size % FXMAC_RX_BUF_UNIT) != 0 {
                    1
                } else {
                    0
                };

                reg |= (rx_buf_size << FXMAC_DMACR_RXBUF_SHIFT) & FXMAC_DMACR_RXBUF_MASK;
                write_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *mut u32, reg);
            } else if queue_num < instance_p.config.max_queue_num {
                let mut rx_buf_size: u32 = 0;
                rx_buf_size = instance_p.max_frame_size / FXMAC_RX_BUF_UNIT;
                rx_buf_size += if (instance_p.max_frame_size % FXMAC_RX_BUF_UNIT) != 0 {
                    1
                } else {
                    0
                };

                write_reg(
                    (FXMAC_IOBASE + FXMAC_RXBUFQX_SIZE_OFFSET(queue_num as u64)) as *mut u32,
                    rx_buf_size & FXMAC_RXBUFQX_SIZE_MASK,
                );
            }
        }

        if (options & FXMAC_SGMII_ENABLE_OPTION) != 0 {
            reg_new_netcfg |= (FXMAC_NWCFG_SGMII_MODE_ENABLE_MASK | FXMAC_NWCFG_PCSSEL_MASK);
        }

        if (options & FXMAC_LOOPBACK_NO_MII_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg |= FXMAC_NWCTRL_LOOPBACK_LOCAL_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        if (options & FXMAC_LOOPBACK_USXGMII_OPTION) != 0 {
            write_reg((FXMAC_IOBASE + FXMAC_TEST_CONTROL_OFFSET) as *mut u32, 2);
        }

        // Officially change the NET_CONFIG registers if it needs to be
        // modified.
        if (reg_netcfg != reg_new_netcfg) {
            write_reg(
                (FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32,
                reg_new_netcfg,
            );
        }

        // Enable TX checksum offload
        if (options & FXMAC_TX_CHKSUM_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *const u32);
            reg |= FXMAC_DMACR_TCPCKSUM_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *mut u32, reg);
        }

        // Enable transmitter
        if (options & FXMAC_TRANSMITTER_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg |= FXMAC_NWCTRL_TXEN_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        // Enable receiver
        if (options & FXMAC_RECEIVER_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg |= FXMAC_NWCTRL_RXEN_MASK;

            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        // The remaining options not handled here are managed elsewhere in the
        // driver. No register modifications are needed at this time. Reflecting
        // the option in instance_p->options is good enough for now.

        // Set options word to its new value
        instance_p.options |= options;

        status = 0; // FT_SUCCESS;
    }

    status
}

/// Clear options for the driver/device
fn FXmacClearOptions(instance_p: &mut FXmac, options: u32, queue_num: u32) -> u32 {
    let mut reg: u32 = 0; /* Generic */
    let mut reg_net_cfg: u32 = 0; /* Reflects original contents of NET_CONFIG */
    let mut reg_new_net_cfg: u32 = 0; /* Reflects new contents of NET_CONFIG */
    let mut status: u32 = 0;

    // let is_started = 0;
    // Be sure device has been stopped
    if (instance_p.is_started == FT_COMPONENT_IS_STARTED) {
        status = 9; //FXMAC_ERR_MAC_IS_PROCESSING
        error!("FXMAC is processing when calling FXmacClearOptions function");
    } else {
        // Many of these options will change the NET_CONFIG registers.
        // To reduce the amount of IO to the device, group these options here
        // and change them all at once.
        // Grab current register contents
        reg_net_cfg = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);
        reg_new_net_cfg = reg_net_cfg;
        // There is only RX configuration!?
        // It is configured in two different length, up to 1536 and 10240 bytes
        if (options & FXMAC_FRAME1536_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_1536RXEN_MASK;
        }

        // Turn off VLAN packet only
        if (options & FXMAC_VLAN_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_NVLANDISC_MASK;
        }

        // Turn off FCS stripping on receive packets
        if (options & FXMAC_FCS_STRIP_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_FCS_REMOVE_MASK;
        }

        // Turn off length/type field checking on receive packets
        if (options & FXMAC_LENTYPE_ERR_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_LENGTH_FIELD_ERROR_FRAME_DISCARD_MASK;
        }

        // Turn off flow control
        if (options & FXMAC_FLOW_CONTROL_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_PAUSE_ENABLE_MASK;
        }

        // Turn off promiscuous frame filtering (all frames are received)
        if (options & FXMAC_PROMISC_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_COPYALLEN_MASK;
        }

        // Disallow broadcast address filtering => broadcast reception
        if (options & FXMAC_BROADCAST_OPTION) != 0 {
            reg_new_net_cfg |= FXMAC_NWCFG_BCASTDI_MASK;
        }

        // Disallow unicast address filtering
        if (options & FXMAC_UNICAST_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_UCASTHASHEN_MASK;
        }

        // Disallow multicast address filtering
        if (options & FXMAC_MULTICAST_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_MCASTHASHEN_MASK;
        }

        if (options & FXMAC_TAIL_PTR_OPTION) != 0 {
            write_reg((FXMAC_IOBASE + FXMAC_TAIL_ENABLE) as *mut u32, 0);
        }

        // Disable RX checksum offload
        if (options & FXMAC_RX_CHKSUM_ENABLE_OPTION) != 0 {
            reg_new_net_cfg &= !FXMAC_NWCFG_RXCHKSUMEN_MASK;
        }

        // Disable jumbo frames
        if (options & FXMAC_JUMBO_ENABLE_OPTION) != 0
        // 恢复之前buffer 容量
        {
            instance_p.max_mtu_size = FXMAC_MTU;
            instance_p.max_frame_size = FXMAC_MAX_FRAME_SIZE;

            reg_new_net_cfg &= !FXMAC_NWCFG_JUMBO_MASK;

            reg = read_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *const u32);

            reg &= !FXMAC_DMACR_RXBUF_MASK;

            if queue_num == 0 {
                let mut rx_buf_size: u32 = 0;

                reg = read_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *const u32);
                reg &= !FXMAC_DMACR_RXBUF_MASK;

                rx_buf_size = instance_p.max_frame_size / FXMAC_RX_BUF_UNIT;
                rx_buf_size += if instance_p.max_frame_size % FXMAC_RX_BUF_UNIT != 0 {
                    1
                } else {
                    0
                };

                reg |= (rx_buf_size << FXMAC_DMACR_RXBUF_SHIFT) & FXMAC_DMACR_RXBUF_MASK;

                write_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *mut u32, reg);
            } else if (queue_num < instance_p.config.max_queue_num) {
                let mut rx_buf_size: u32 = 0;
                rx_buf_size = instance_p.max_frame_size / FXMAC_RX_BUF_UNIT;
                rx_buf_size += if (instance_p.max_frame_size % FXMAC_RX_BUF_UNIT) != 0 {
                    1
                } else {
                    0
                };

                write_reg(
                    (FXMAC_IOBASE + FXMAC_RXBUFQX_SIZE_OFFSET(queue_num as u64)) as *mut u32,
                    rx_buf_size & FXMAC_RXBUFQX_SIZE_MASK,
                );
            }
        }

        if (options & FXMAC_SGMII_ENABLE_OPTION) != 0 {
            reg_new_net_cfg &= !(FXMAC_NWCFG_SGMII_MODE_ENABLE_MASK | FXMAC_NWCFG_PCSSEL_MASK);
        }

        if (options & FXMAC_LOOPBACK_NO_MII_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg &= !FXMAC_NWCTRL_LOOPBACK_LOCAL_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        if (options & FXMAC_LOOPBACK_USXGMII_OPTION) != 0 {
            write_reg(
                (FXMAC_IOBASE + FXMAC_TEST_CONTROL_OFFSET) as *mut u32,
                read_reg((FXMAC_IOBASE + FXMAC_TEST_CONTROL_OFFSET) as *const u32) & !2,
            );
        }

        // Officially change the NET_CONFIG registers if it needs to be
        // modified.
        if reg_net_cfg != reg_new_net_cfg {
            write_reg(
                (FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32,
                reg_new_net_cfg,
            );
        }

        // Disable TX checksum offload
        if (options & FXMAC_TX_CHKSUM_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *const u32);
            reg &= !FXMAC_DMACR_TCPCKSUM_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_DMACR_OFFSET) as *mut u32, reg);
        }

        // Disable transmitter
        if (options & FXMAC_TRANSMITTER_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg &= !FXMAC_NWCTRL_TXEN_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        // Disable receiver
        if (options & FXMAC_RECEIVER_ENABLE_OPTION) != 0 {
            reg = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            reg &= !FXMAC_NWCTRL_RXEN_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, reg);
        }

        // The remaining options not handled here are managed elsewhere in the
        // driver. No register modifications are needed at this time. Reflecting
        // option in instance_p->options is good enough for now.

        // Set options word to its new value
        instance_p.options &= !options;

        status = 0; // FT_SUCCESS
    }
    status
}

///  Clear the Hash registers for the mac address pointed by address_ptr.
fn FXmacClearHash() {
    write_reg((FXMAC_IOBASE + FXMAC_HASHL_OFFSET) as *mut u32, 0);

    // write bits [63:32] in TOP
    write_reg((FXMAC_IOBASE + FXMAC_HASHH_OFFSET) as *mut u32, 0);
}

/// Sets the MAC address for the specified address slot.
///
/// The device must be stopped before calling this function.
///
/// # Arguments
///
/// * `address_ptr` - 6-byte MAC address.
/// * `index` - Address slot index (0..FXMAC_MAX_MAC_ADDR).
///
/// # Panics
///
/// Panics if `index` is out of range.
pub fn FXmacSetMacAddress(address_ptr: &[u8; 6], index: u8) -> u32 {
    let mut mac_addr: u32 = 0;
    let aptr = address_ptr;
    let index_loc: u8 = index;
    let mut status: u32 = 0;
    assert!(
        (index_loc < FXMAC_MAX_MAC_ADDR as u8),
        "index of Mac Address exceed {}",
        FXMAC_MAX_MAC_ADDR
    );

    let is_started = 0;
    // Be sure device has been stopped
    if is_started == FT_COMPONENT_IS_STARTED {
        // status = FXMAC_ERR_MAC_IS_PROCESSING;
        status = 9;
        error!("FXMAC is processing when calling FXmacSetMacAddress function");
    } else {
        // Set the MAC bits [31:0] in BOT
        mac_addr = aptr[0] as u32
            | ((aptr[1] as u32) << 8)
            | ((aptr[2] as u32) << 16)
            | ((aptr[3] as u32) << 24);
        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_SA1B as u64 + (index_loc * 8) as u64) as *mut u32,
            mac_addr,
        );

        // There are reserved bits in TOP so don't affect them
        mac_addr =
            read_reg((FXMAC_IOBASE + FXMAC_GEM_SA1T as u64 + (index_loc * 8) as u64) as *const u32);
        mac_addr &= !FXMAC_GEM_SAB_MASK;

        // Set MAC bits [47:32] in TOP
        mac_addr |= aptr[4] as u32;
        mac_addr |= (aptr[5] as u32) << 8;

        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_SA1T as u64 + (index_loc * 8) as u64) as *mut u32,
            mac_addr,
        );

        status = 0; // FT_SUCCESS
    }

    status
}
/// Reads a MAC address from the specified address slot.
///
/// # Arguments
///
/// * `address_ptr` - Output buffer for the MAC address.
/// * `index` - Address slot index (0..FXMAC_MAX_MAC_ADDR).
///
/// # Panics
///
/// Panics if `index` is out of range.
pub fn FXmacGetMacAddress(address_ptr: &mut [u8; 6], index: u8) {
    assert!((index as u32) < FXMAC_MAX_MAC_ADDR);

    let mut reg_value: u32 =
        read_reg((FXMAC_IOBASE + FXMAC_GEM_SA1B as u64 + (index as u64 * 8)) as *const u32);
    address_ptr[0] = reg_value as u8;
    address_ptr[1] = (reg_value >> 8) as u8;
    address_ptr[2] = (reg_value >> 16) as u8;
    address_ptr[3] = (reg_value >> 24) as u8;

    reg_value = read_reg((FXMAC_IOBASE + FXMAC_GEM_SA1T as u64 + (index as u64 * 8)) as *const u32);
    address_ptr[4] = (reg_value) as u8;
    address_ptr[5] = (reg_value >> 8) as u8;
}

/// Sets a 48-bit MAC address entry in the hash table.
///
/// The device must be stopped before calling this function.
///
/// # Arguments
///
/// * `intance_p` - Mutable reference to the FXMAC instance.
/// * `mac_address` - The MAC address to hash.
///
/// The hash address register is 64 bits long and takes up two locations in
/// the memory map. The least significant bits are stored in hash register
/// bottom and the most significant bits in hash register top.
///
/// The unicast hash enable and the multicast hash enable bits in the network
/// configuration register enable the reception of hash matched frames. The
/// destination address is reduced to a 6 bit index into the 64 bit hash
/// register using the following hash function. The hash function is an XOR
/// of every sixth bit of the destination address.
pub fn FXmac_SetHash(intance_p: &mut FXmac, mac_address: &[u8; 6]) -> u32 {
    let mut HashAddr: u32 = 0;
    let mut Status: u32 = 0;
    debug!("Set MAC: {:x?} in hash table", mac_address);

    // Check that the Ethernet address (MAC) is not 00:00:00:00:00:00
    assert!(!((mac_address[0] == 0) && (mac_address[5] == 0)));
    assert!(intance_p.is_ready == FT_COMPONENT_IS_READY);

    // Be sure device has been stopped
    if (intance_p.is_started == FT_COMPONENT_IS_STARTED) {
        error!("FXmac_SetHash failed: FXMAC_ERR_MAC_IS_PROCESSING");
        Status = 9; // FXMAC_ERR_MAC_IS_PROCESSING
    } else {
        let Temp1: u8 = (mac_address[0]) & 0x3F;
        let Temp2: u8 = ((mac_address[0] >> 6) & 0x03) | ((mac_address[1] & 0x0F) << 2);
        let Temp3: u8 = ((mac_address[1] >> 4) & 0x0F) | ((mac_address[2] & 0x3) << 4);
        let Temp4: u8 = ((mac_address[2] >> 2) & 0x3F);
        let Temp5: u8 = mac_address[3] & 0x3F;
        let Temp6: u8 = ((mac_address[3] >> 6) & 0x03) | ((mac_address[4] & 0x0F) << 2);
        let Temp7: u8 = ((mac_address[4] >> 4) & 0x0F) | ((mac_address[5] & 0x03) << 4);
        let Temp8: u8 = ((mac_address[5] >> 2) & 0x3F);

        let Result: u32 = (Temp1 as u32)
            ^ (Temp2 as u32)
            ^ (Temp3 as u32)
            ^ (Temp4 as u32)
            ^ (Temp5 as u32)
            ^ (Temp6 as u32)
            ^ (Temp7 as u32)
            ^ (Temp8 as u32);

        if (Result >= FXMAC_MAX_HASH_BITS) {
            Status = 1; // FXMAC_ERR_INVALID_PARAM
        } else {
            if (Result < 32) {
                HashAddr =
                    read_reg((intance_p.config.base_address + FXMAC_HASHL_OFFSET) as *const u32);
                HashAddr |= 1 << Result;
                write_reg(
                    (intance_p.config.base_address + FXMAC_HASHL_OFFSET) as *mut u32,
                    HashAddr,
                );
            } else {
                HashAddr =
                    read_reg((intance_p.config.base_address + FXMAC_HASHH_OFFSET) as *const u32);
                HashAddr |= 1 << (Result - 32);
                write_reg(
                    (intance_p.config.base_address + FXMAC_HASHH_OFFSET) as *mut u32,
                    HashAddr,
                );
            }
            Status = 0;
        }
    }

    Status
}

/// Delete 48-bit MAC addresses in hash table.
/// The device must be stopped before calling this function.
pub fn FXmac_DeleteHash(intance_p: &mut FXmac, mac_address: &[u8; 6]) -> u32 {
    let mut HashAddr: u32 = 0;
    let mut Status: u32 = 0;

    assert!(intance_p.is_ready == FT_COMPONENT_IS_READY);

    // Be sure device has been stopped
    if (intance_p.is_started == FT_COMPONENT_IS_STARTED) {
        Status = 9; // (FXMAC_ERR_MAC_IS_PROCESSING);
    } else {
        let mut Temp1: u8 = (mac_address[0]) & 0x3F;
        let mut Temp2: u8 = ((mac_address[0] >> 6) & 0x03) | ((mac_address[1] & 0x0F) << 2);
        let mut Temp3: u8 = ((mac_address[1] >> 4) & 0x0F) | ((mac_address[2] & 0x03) << 4);
        let mut Temp4: u8 = ((mac_address[2] >> 2) & 0x3F);
        let mut Temp5: u8 = (mac_address[3]) & 0x3F;
        let mut Temp6: u8 = ((mac_address[3] >> 6) & 0x03) | ((mac_address[4] & 0x0F) << 2);
        let mut Temp7: u8 = ((mac_address[4] >> 4) & 0x0F) | ((mac_address[5] & 0x03) << 4);
        let mut Temp8: u8 = ((mac_address[5] >> 2) & 0x3F);

        let Result: u32 = (Temp1 as u32)
            ^ (Temp2 as u32)
            ^ (Temp3 as u32)
            ^ (Temp4 as u32)
            ^ (Temp5 as u32)
            ^ (Temp6 as u32)
            ^ (Temp7 as u32)
            ^ (Temp8 as u32);

        if Result >= FXMAC_MAX_HASH_BITS {
            Status = 1; //(FXMAC_ERR_INVALID_PARAM);
        } else {
            if Result < 32 {
                HashAddr =
                    read_reg((intance_p.config.base_address + FXMAC_HASHL_OFFSET) as *const u32);
                HashAddr &= !((1 << Result) as u32);
                write_reg(
                    (intance_p.config.base_address + FXMAC_HASHL_OFFSET) as *mut u32,
                    HashAddr,
                );
            } else {
                HashAddr =
                    read_reg((intance_p.config.base_address + FXMAC_HASHH_OFFSET) as *const u32);
                HashAddr &= !((1 << (Result - 32)) as u32);
                write_reg(
                    (intance_p.config.base_address + FXMAC_HASHH_OFFSET) as *mut u32,
                    HashAddr,
                );
            }
            Status = 0;
        }
    }
    Status
}

/// Set the Type ID match for this driver/device.  The register is a 32-bit value.
/// The device must be stopped before calling this function.
fn FXmacSetTypeIdCheck(id_check: u32, index: u8) -> u32 {
    let mut status: u32 = 0;
    assert!(
        (index < FXMAC_MAX_TYPE_ID as u8),
        "index of Type ID exceed {}",
        FXMAC_MAX_TYPE_ID
    );

    let is_started = 0;
    // Be sure device has been stopped
    if is_started == FT_COMPONENT_IS_STARTED {
        status = 9; //FXMAC_ERR_MAC_IS_PROCESSING
        error!("FXMAC is processing when calling FXmacSetTypeIdCheck function");
    } else {
        // Set the ID bits in MATCHx register
        write_reg(
            (FXMAC_IOBASE + FXMAC_MATCH1_OFFSET + (index * 4) as u64) as *mut u32,
            id_check,
        );

        status = FT_SUCCESS;
    }

    status
}

/// FXmacSelectClk
/// Determine the driver clock configuration based on the media independent interface
/// FXMAC_CLK_TYPE_0
fn FXmacSelectClk(instance_p: &mut FXmac) {
    let speed: u32 = instance_p.config.speed;
    let FXMAC_WRITEREG32 = |base_address: u64, offset: u32, reg_value: u32| {
        write_reg((base_address + offset as u64) as *mut u32, reg_value)
    };

    assert!(
        (speed == FXMAC_SPEED_10)
            || (speed == FXMAC_SPEED_100)
            || (speed == FXMAC_SPEED_1000)
            || (speed == FXMAC_SPEED_2500)
            || (speed == FXMAC_SPEED_10000)
    );

    if (instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_USXGMII)
        || (instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_XGMII)
    {
        if speed == FXMAC_SPEED_10000 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x4); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x1); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x1,
            ); /*0x1c10*/
        } else if speed == FXMAC_SPEED_5000 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x8); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x2); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0,
            ); /*0x1c10*/
        }
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_5GBASER {
        if speed == FXMAC_SPEED_5000 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x8); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x2); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x0,
            ); /*0x1c10*/
        }
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_2500BASEX {
        if speed == FXMAC_SPEED_25000 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x1); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x2); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x1,
            ); /*0x1c10*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x1); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x1); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x1); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x1); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x0); /*0x1c34*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3_0, 0x0); /*0x1c70*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL4_0, 0x0); /*0x1c74*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL3_0, 0x0); /*0x1c78*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL4_0, 0x0);
            // 0x1c7c
        }
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_SGMII {
        info!("FXMAC_PHY_INTERFACE_MODE_SGMII init");
        if speed == FXMAC_SPEED_2500 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x1);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x2);
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x1,
            );
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x1);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x1);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x1);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x1);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x0);
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3_0, 0x0); /*0x1c70*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL4_0, 0x0); /*0x1c74*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL3_0, 0x0); /*0x1c78*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL4_0, 0x0);
        // 0x1c7c
        } else if speed == FXMAC_SPEED_1000 {
            info!("sgmii FXMAC_SPEED_1000");
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x4); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x8); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x1,
            ); /*0x1c10*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0x0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x0); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x0); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x1); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x1); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x0); /*0x1c34*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3_0, 0x0); /*0x1c70*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL4_0, 0x0); /*0x1c74*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL3_0, 0x0); /*0x1c78*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL4_0, 0x0);
        // 0x1c7c
        } else if (speed == FXMAC_SPEED_100) || (speed == FXMAC_SPEED_10) {
            info!("sgmii FXMAC_SPEED_{}", speed);

            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_SRC_SEL_LN, 0x1); /*0x1c04*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL0_LN, 0x4); /*0x1c08*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_DIV_SEL1_LN, 0x8); /*0x1c0c*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_PMA_XCVR_POWER_STATE,
                0x1,
            ); /*0x1c10*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0x0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x0); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x1); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x1); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x1); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x0); /*0x1c34*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3_0, 0x1); /*0x1c70*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL4_0, 0x0); /*0x1c74*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL3_0, 0x0); /*0x1c78*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL4_0, 0x1);
            // 0x1c7c
        }
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_RGMII {
        info!("FXMAC_PHY_INTERFACE_MODE_RGMII init");
        if speed == FXMAC_SPEED_1000 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_MII_SELECT, 0x1); /*0x1c18*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_SEL_MII_ON_RGMII,
                0x0,
            ); /*0x1c1c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0x0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x1); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x0); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x0); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x0); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x1); /*0x1c34*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_CLK_250M_DIV10_DIV100_SEL,
                0x0,
            ); /*0x1c38*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL5, 0x1); /*0x1c48*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL0,
                0x1,
            ); /*0x1c80*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL1,
                0x0,
            ); /*0x1c84*/
        } else if speed == FXMAC_SPEED_100 {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_MII_SELECT, 0x1); /*0x1c18*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_SEL_MII_ON_RGMII,
                0x0,
            ); /*0x1c1c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0x0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x1); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x0); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x0); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x0); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x1); /*0x1c34*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_CLK_250M_DIV10_DIV100_SEL,
                0x0,
            ); /*0x1c38*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL5, 0x1); /*0x1c48*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL0,
                0x0,
            ); /*0x1c80*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL1,
                0x0,
            ); /*0x1c84*/
        } else {
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_MII_SELECT, 0x1); /*0x1c18*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_SEL_MII_ON_RGMII,
                0x0,
            ); /*0x1c1c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL0, 0x0); /*0x1c20*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL1, 0x1); /*0x1c24*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL2, 0x0); /*0x1c28*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_TX_CLK_SEL3, 0x0); /*0x1c2c*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL0, 0x0); /*0x1c30*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL1, 0x1); /*0x1c34*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_CLK_250M_DIV10_DIV100_SEL,
                0x1,
            ); /*0x1c38*/
            FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL5, 0x1); /*0x1c48*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL0,
                0x0,
            ); /*0x1c80*/
            FXMAC_WRITEREG32(
                instance_p.config.base_address,
                FXMAC_GEM_RGMII_TX_CLK_SEL1,
                0x0,
            ); /*0x1c84*/
        }
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_RMII {
        FXMAC_WRITEREG32(instance_p.config.base_address, FXMAC_GEM_RX_CLK_SEL5, 0x1);
        // 0x1c48
    }

    FXmacHighSpeedConfiguration(instance_p, speed);
}

fn FXmacHighSpeedConfiguration(instance_p: &mut FXmac, speed: u32) {
    let mut reg_value: u32 = 0;
    let mut set_speed: i32 = 0;
    match speed {
        FXMAC_SPEED_25000 => {
            set_speed = 2;
        }
        FXMAC_SPEED_10000 => {
            set_speed = 4;
        }
        FXMAC_SPEED_5000 => {
            set_speed = 3;
        }
        FXMAC_SPEED_2500 => {
            set_speed = 2;
        }
        FXMAC_SPEED_1000 => {
            set_speed = 1;
        }
        _ => {
            set_speed = 0;
        }
    }

    // GEM_HSMAC(0x0050) provide rate to the external
    reg_value = read_reg((FXMAC_IOBASE + FXMAC_GEM_HSMAC as u64) as *const u32);
    reg_value &= !FXMAC_GEM_HSMACSPEED_MASK;
    reg_value |= (set_speed as u32) & FXMAC_GEM_HSMACSPEED_MASK;
    write_reg(
        (FXMAC_IOBASE + FXMAC_GEM_HSMAC as u64) as *mut u32,
        reg_value,
    );

    reg_value = read_reg((FXMAC_IOBASE + FXMAC_GEM_HSMAC as u64) as *const u32);

    info!("FXMAC_GEM_HSMAC is {:#x}", reg_value);
}

/// FXmacInitInterface
/// Initialize the MAC controller configuration based on the PHY interface type
fn FXmacInitInterface(instance_p: &mut FXmac) {
    let mut config: u32 = 0;
    let mut control: u32 = 0;

    info!(
        "FXmacInitInterface, PHY MODE:{:?}",
        instance_p.config.interface
    );

    if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_XGMII {
        config = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);
        config &= !FXMAC_NWCFG_PCSSEL_MASK;
        write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, config);

        control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
        control |= FXMAC_NWCTRL_ENABLE_HS_MAC_MASK; /* Use high speed MAC */
        write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);

        instance_p.config.duplex = 1;
    } else if (instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_USXGMII)
        || (instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_5GBASER)
    {
        info!("usx interface is {:?}", instance_p.config.interface);
        // network_config
        instance_p.config.duplex = 1;
        config = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);
        config |= FXMAC_NWCFG_PCSSEL_MASK;
        config &= !FXMAC_NWCFG_100_MASK;
        config &= !FXMAC_NWCFG_SGMII_MODE_ENABLE_MASK;
        if (instance_p.config.duplex == 1) {
            info!("is duplex");
            config |= FXMAC_NWCFG_FDEN_MASK;
        }

        write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, config);

        // network_control
        control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
        control |= FXMAC_NWCTRL_ENABLE_HS_MAC_MASK; /* Use high speed MAC */
        write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);

        // High speed PCS control register
        control = read_reg((FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *const u32);

        if (instance_p.config.speed == FXMAC_SPEED_10000) {
            info!("is 10G");
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_10G;
            control |= FXMAC_GEM_USX_SERDES_RATE_10G;
        } else if (instance_p.config.speed == FXMAC_SPEED_25000) {
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_2_5G;
        } else if (instance_p.config.speed == FXMAC_SPEED_1000) {
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_1G;
        } else if (instance_p.config.speed == FXMAC_SPEED_100) {
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_100M;
        } else if (instance_p.config.speed == FXMAC_SPEED_5000) {
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_5G;
            control |= FXMAC_GEM_USX_SERDES_RATE_5G;
        }

        control &= !(FXMAC_GEM_USX_TX_SCR_BYPASS | FXMAC_GEM_USX_RX_SCR_BYPASS);
        control |= FXMAC_GEM_USX_RX_SYNC_RESET;
        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *mut u32,
            control,
        );

        control = read_reg((FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *const u32);
        control &= !FXMAC_GEM_USX_RX_SYNC_RESET;
        control |= FXMAC_GEM_USX_TX_DATAPATH_EN;
        control |= FXMAC_GEM_USX_SIGNAL_OK;

        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *mut u32,
            control,
        );
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_2500BASEX {
        // network_config
        instance_p.config.duplex = 1;
        config = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);
        config |= FXMAC_NWCFG_PCSSEL_MASK | FXMAC_NWCFG_SGMII_MODE_ENABLE_MASK;
        config &= !FXMAC_NWCFG_100_MASK;

        if (instance_p.config.duplex == 1) {
            config |= FXMAC_NWCFG_FDEN_MASK;
        }
        write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, config);

        // network_control
        control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
        control &= !FXMAC_NWCTRL_ENABLE_HS_MAC_MASK;
        control |= FXMAC_NWCTRL_TWO_PT_FIVE_GIG_MASK; /* Use high speed MAC */
        write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);

        // High speed PCS control register
        control = read_reg((FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *const u32);

        if (instance_p.config.speed == FXMAC_SPEED_25000) {
            control |= FXMAC_GEM_USX_HS_MAC_SPEED_2_5G;
        }

        control &= !(FXMAC_GEM_USX_TX_SCR_BYPASS | FXMAC_GEM_USX_RX_SCR_BYPASS);
        control |= FXMAC_GEM_USX_RX_SYNC_RESET;
        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *mut u32,
            control,
        );

        control = read_reg((FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *const u32);
        control &= !FXMAC_GEM_USX_RX_SYNC_RESET;
        control |= FXMAC_GEM_USX_TX_DATAPATH_EN;
        control |= FXMAC_GEM_USX_SIGNAL_OK;

        write_reg(
            (FXMAC_IOBASE + FXMAC_GEM_USX_CONTROL_OFFSET) as *mut u32,
            control,
        );
    } else if instance_p.config.interface == FXmacPhyInterface::FXMAC_PHY_INTERFACE_MODE_SGMII {
        config = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);
        config |= FXMAC_NWCFG_PCSSEL_MASK | FXMAC_NWCFG_SGMII_MODE_ENABLE_MASK;

        config &= !(FXMAC_NWCFG_100_MASK
            | FXMAC_NWCFG_FDEN_MASK
            | FXMAC_NWCFG_LENGTH_FIELD_ERROR_FRAME_DISCARD_MASK);

        if instance_p.moudle_id >= 2 {
            config &= !FXMAC_NWCFG_1000_MASK;
        }

        if instance_p.config.duplex != 0 {
            config |= FXMAC_NWCFG_FDEN_MASK;
        }

        if instance_p.config.speed == FXMAC_SPEED_100 {
            config |= FXMAC_NWCFG_100_MASK;
        } else if instance_p.config.speed == FXMAC_SPEED_1000 {
            config |= FXMAC_NWCFG_1000_MASK;
        }

        write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, config);

        if instance_p.config.speed == FXMAC_SPEED_2500 {
            control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            control |= FXMAC_NWCTRL_TWO_PT_FIVE_GIG_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);
        } else {
            control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
            control &= !FXMAC_NWCTRL_TWO_PT_FIVE_GIG_MASK;
            write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);
        }

        control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
        control &= !FXMAC_NWCTRL_ENABLE_HS_MAC_MASK;
        write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);

        control = read_reg((FXMAC_IOBASE + FXMAC_PCS_CONTROL_OFFSET) as *const u32);
        control |= FXMAC_PCS_CONTROL_ENABLE_AUTO_NEG;
        write_reg(
            (FXMAC_IOBASE + FXMAC_PCS_CONTROL_OFFSET) as *mut u32,
            control,
        );
    } else {
        config = read_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *const u32);

        info!("select rgmii");

        config &= !FXMAC_NWCFG_PCSSEL_MASK;
        config &= !(FXMAC_NWCFG_100_MASK | FXMAC_NWCFG_FDEN_MASK);

        if instance_p.moudle_id >= 2 {
            config &= !FXMAC_NWCFG_1000_MASK;
        }

        if instance_p.config.duplex != 0 {
            config |= FXMAC_NWCFG_FDEN_MASK;
        }

        if instance_p.config.speed == FXMAC_SPEED_100 {
            config |= FXMAC_NWCFG_100_MASK;
        } else if instance_p.config.speed == FXMAC_SPEED_1000 {
            config |= FXMAC_NWCFG_1000_MASK;
        }

        write_reg((FXMAC_IOBASE + FXMAC_NWCFG_OFFSET) as *mut u32, config);

        control = read_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *const u32);
        control &= !FXMAC_NWCTRL_ENABLE_HS_MAC_MASK; /* Use high speed MAC */
        write_reg((FXMAC_IOBASE + FXMAC_NWCTRL_OFFSET) as *mut u32, control);
    }
}