summaryrefslogtreecommitdiff
path: root/bsp/nxp/imx/imxrt/libraries/drivers/drv_netc.c
blob: 6c4dfb1a82d968e204a6786af51b81422c3d34c2 (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
/*
 * Copyright (c) 2006-2026, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2026-06-05     Tim        First version for i.MXRT1180 NETC EP0
 * 2026-06-09     Tim        Add Switch support
 */

#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include "board.h"

#ifdef RT_USING_LWIP

#include <lwip/pbuf.h>
#include <netif/ethernetif.h>

#ifndef NETC_RX_ZERO_COPY
#define NETC_RX_ZERO_COPY 1
#endif

#ifndef NETC_USE_SWT
#define NETC_USE_SWT 0
#endif

#if NETC_RX_ZERO_COPY && !LWIP_SUPPORT_CUSTOM_PBUF
#error "NETC RX zero-copy requires LWIP_SUPPORT_CUSTOM_PBUF=1 in lwipopts.h"
#endif

#include "fsl_common.h"
#include "fsl_clock.h"
#include "fsl_netc_soc.h"
#include "fsl_netc_endpoint.h"
#if NETC_USE_SWT
#if defined(FSL_FEATURE_NETC_HAS_NO_SWITCH) && FSL_FEATURE_NETC_HAS_NO_SWITCH
#error "NETC_USE_SWT requires NETC switch hardware support"
#endif
#include "fsl_netc_switch.h"
#endif
#if defined(FSL_FEATURE_NETC_HAS_SWITCH_TAG) && FSL_FEATURE_NETC_HAS_SWITCH_TAG
#include "fsl_netc_tag.h"
#endif
#include "fsl_netc_mdio.h"
#include "fsl_msgintr.h"

#define LOG_TAG "drv.netc"
#include "drv_log.h"

#define NETC_RING_ID            0U
#define NETC_RXBD_NUM           8U
#define NETC_TXBD_NUM           8U
#define NETC_CMDBD_NUM          8U
#define NETC_RXBUFF_SIZE        1536U
#define NETC_BUFF_ALIGNMENT     64U
#define NETC_FRAME_MAX_FRAMELEN 1518U
#define NETC_RXBUFF_ALIGNED_SIZE \
    (((NETC_RXBUFF_SIZE + NETC_BUFF_ALIGNMENT - 1U) / NETC_BUFF_ALIGNMENT) * NETC_BUFF_ALIGNMENT)
#define NETC_MAX_BUFFERS_PER_FRAME \
    ((NETC_FRAME_MAX_FRAMELEN + NETC_RXBUFF_ALIGNED_SIZE - 1U) / NETC_RXBUFF_ALIGNED_SIZE)
#define NETC_PHY_STATUS_REG      0x01U
#define NETC_PHY_LINK_UP_MASK    0x0004U
#define NETC_PHY_POLL_DELAY_TICK (RT_TICK_PER_SECOND)
#define NETC_TX_TIMEOUT_TICK     (RT_TICK_PER_SECOND)

#if NETC_RXBD_NUM < NETC_MAX_BUFFERS_PER_FRAME
#error "NETC_RXBD_NUM < NETC_MAX_BUFFERS_PER_FRAME"
#endif

#ifndef NETC_RXBUFF_NUM
#define NETC_RXBUFF_NUM (NETC_RXBD_NUM + 5U)
#endif

#if NETC_RX_ZERO_COPY && (NETC_RXBUFF_NUM < (NETC_RXBD_NUM + NETC_MAX_BUFFERS_PER_FRAME))
#error "NETC_RXBUFF_NUM < (NETC_RXBD_NUM + NETC_MAX_BUFFERS_PER_FRAME)"
#endif

#if NETC_RX_ZERO_COPY
#define NETC_RXBUFF_TOTAL_NUM NETC_RXBUFF_NUM
#else
#define NETC_RXBUFF_TOTAL_NUM NETC_RXBD_NUM
#endif

#define NETC_MII_MODE    kNETC_RmiiMode
#define NETC_LINK_SPEED  kNETC_MiiSpeed100M
#define NETC_LINK_DUPLEX kNETC_MiiFullDuplex

#if NETC_USE_SWT
#define NETC_PSI kNETC_ENETC1PSI0
#ifndef NETC_PHY_ADDR
#define NETC_PHY_ADDR BOARD_SWT_PORT0_PHY_ADDR
#endif
#ifndef NETC_SWT_MAX_PORT_NUM
#define NETC_SWT_MAX_PORT_NUM 4U
#endif
#ifndef NETC_SWT_USED_PORT_BITMAP
#define NETC_SWT_USED_PORT_BITMAP 0x5U
#endif
#ifndef NETC_SWT_PSEUDO_PORT
#define NETC_SWT_PSEUDO_PORT 0x4U
#endif
#ifndef NETC_SWT_TX_PORT
#define NETC_SWT_TX_PORT kNETC_SWITCH0Port0
#endif
#else
#define NETC_PSI kNETC_ENETC0PSI0
#ifndef NETC_PHY_ADDR
#define NETC_PHY_ADDR BOARD_EP0_PHY_ADDR
#endif
#endif

#define NETC_MSGINTR     MSGINTR1
#define NETC_MSGINTR_IRQ MSGINTR1_IRQn

#define NETC_MSIX_ENTRY_NUM   2U
#define TX_MSIX_ENTRY_IDX     0U
#define RX_MSIX_ENTRY_IDX     1U
#define SI_COM_MSIX_ENTRY_IDX 2U
#define TX_INTR_MSG_DATA      1U
#define RX_INTR_MSG_DATA      2U
#define SI_COM_INTR_MSG_DATA  3U

#define NETC_DEFAULT_MAC0 0x54U
#define NETC_DEFAULT_MAC1 0x27U
#define NETC_DEFAULT_MAC2 0x8DU
#define NETC_DEFAULT_MAC3 0x11U
#define NETC_DEFAULT_MAC4 0x80U
#define NETC_DEFAULT_MAC5 0x00U

#ifndef RT_NTCP_THREAD_STACK_SIZE
#define RT_NTCP_THREAD_STACK_SIZE 4096
#endif

#ifndef RT_NTCP_THREAD_PRIORITY
#define RT_NTCP_THREAD_PRIORITY 15
#endif

struct rt_imxrt_netc
{
    struct eth_device parent;

    ep_handle_t ep_handle;
#if NETC_USE_SWT
    swt_handle_t swt_handle;
#endif
    netc_mdio_handle_t mdio_handle;

    struct rt_mutex lock;
    struct rt_semaphore tx_isr_sem;

    rt_bool_t hw_inited;
    rt_bool_t mdio_inited;
    rt_bool_t link_up;
#if NETC_USE_SWT
    rt_bool_t swt_inited;
#endif

    rt_uint8_t dev_addr[6];
};

#if NETC_RX_ZERO_COPY
typedef struct netc_rx_pbuf_wrapper
{
    struct pbuf_custom custom;
    void *buffer;
    volatile rt_bool_t buffer_used;
#if NETC_USE_SWT
    swt_handle_t *handle;
#else
    ep_handle_t *handle;
#endif
    rt_uint8_t ring;
} netc_rx_pbuf_wrapper_t;
#endif

AT_NONCACHEABLE_SECTION_ALIGN(static netc_rx_bd_t g_netc_rx_bd[NETC_RXBD_NUM], 128);
AT_NONCACHEABLE_SECTION_ALIGN(static netc_tx_bd_t g_netc_tx_bd[NETC_TXBD_NUM], 128);
AT_NONCACHEABLE_SECTION_ALIGN(static netc_cmd_bd_t g_netc_cmd_bd[NETC_CMDBD_NUM], 128);
#if NETC_USE_SWT
AT_NONCACHEABLE_SECTION_ALIGN(static netc_cmd_bd_t g_netc_swt_cmd_bd[NETC_CMDBD_NUM], 128);
#endif
AT_NONCACHEABLE_SECTION_ALIGN(static rt_uint8_t g_netc_rx_buf[NETC_RXBUFF_TOTAL_NUM][NETC_RXBUFF_SIZE],
                              NETC_BUFF_ALIGNMENT);

static uint64_t g_netc_rx_buf_addr[NETC_RXBD_NUM];
static netc_tx_frame_info_t g_netc_tx_dirty[NETC_TXBD_NUM];
static netc_msix_entry_t g_netc_msix_entry[NETC_MSIX_ENTRY_NUM];

#if NETC_USE_SWT
static swt_config_t g_netc_swt_config;
static swt_transfer_config_t g_netc_swt_transfer_config;
#endif

#if NETC_RX_ZERO_COPY
static netc_rx_pbuf_wrapper_t g_netc_rx_pbufs[NETC_RXBUFF_TOTAL_NUM];
#endif

static struct rt_imxrt_netc imxrt_netc_device;

#if NETC_RX_ZERO_COPY
#if NETC_USE_SWT
static void *_netc_rx_buff_alloc(swt_handle_t *handle, uint32_t length, void *userData)
#else
static void *_netc_rx_buff_alloc(ep_handle_t *handle, uint8_t ring, uint32_t length, void *userData)
#endif
{
    netc_rx_pbuf_wrapper_t *rx_pbufs = (netc_rx_pbuf_wrapper_t *)userData;
    void *buffer = RT_NULL;
    rt_base_t level;
    rt_uint32_t i;

    RT_UNUSED(handle);
#if !NETC_USE_SWT
    RT_UNUSED(ring);
#endif

    if ((rx_pbufs == RT_NULL) || (length > NETC_RXBUFF_SIZE))
    {
        return RT_NULL;
    }

    level = rt_hw_interrupt_disable();
    for (i = 0; i < NETC_RXBUFF_TOTAL_NUM; i++)
    {
        if (!rx_pbufs[i].buffer_used)
        {
            rx_pbufs[i].buffer_used = RT_TRUE;
            buffer = rx_pbufs[i].buffer;
            break;
        }
    }
    rt_hw_interrupt_enable(level);

    return buffer;
}

#if NETC_USE_SWT
static void _netc_rx_buff_free(swt_handle_t *handle, void *address, void *userData)
#else
static void _netc_rx_buff_free(ep_handle_t *handle, uint8_t ring, void *address, void *userData)
#endif
{
    netc_rx_pbuf_wrapper_t *rx_pbufs = (netc_rx_pbuf_wrapper_t *)userData;
    rt_base_t level;
    rt_uint32_t i;

#if !NETC_USE_SWT
    RT_UNUSED(ring);
#endif

    if ((rx_pbufs == RT_NULL) && (handle != RT_NULL))
    {
        rx_pbufs = (netc_rx_pbuf_wrapper_t *)handle->cfg.userData;
    }

    if ((rx_pbufs == RT_NULL) || (address == RT_NULL))
    {
        return;
    }

    level = rt_hw_interrupt_disable();
    for (i = 0; i < NETC_RXBUFF_TOTAL_NUM; i++)
    {
        if (rx_pbufs[i].buffer == address)
        {
            rx_pbufs[i].buffer_used = RT_FALSE;
            break;
        }
    }
    rt_hw_interrupt_enable(level);

    if (i >= NETC_RXBUFF_TOTAL_NUM)
    {
        LOG_W("unknown NETC Rx zero-copy buffer: %p", address);
    }
}

static void _netc_rx_pbuf_free(struct pbuf *p)
{
    netc_rx_pbuf_wrapper_t *rx_pbuf = (netc_rx_pbuf_wrapper_t *)p;

#if NETC_USE_SWT
    _netc_rx_buff_free(rx_pbuf->handle, rx_pbuf->buffer, RT_NULL);
#else
    _netc_rx_buff_free(rx_pbuf->handle, rx_pbuf->ring, rx_pbuf->buffer, RT_NULL);
#endif
}

static netc_rx_pbuf_wrapper_t *_netc_find_rx_pbuf(void *buffer)
{
    rt_uint32_t i;

    for (i = 0; i < NETC_RXBUFF_TOTAL_NUM; i++)
    {
        if (g_netc_rx_pbufs[i].buffer == buffer)
        {
            return &g_netc_rx_pbufs[i];
        }
    }

    return RT_NULL;
}

static void _netc_rx_release_frame_buffers(struct rt_imxrt_netc *netc,
                                           netc_frame_struct_t *frame,
                                           rt_uint16_t start)
{
    rt_uint16_t i;

    for (i = start; i < frame->length; i++)
    {
        if (frame->buffArray[i].buffer != RT_NULL)
        {
#if NETC_USE_SWT
            _netc_rx_buff_free(&netc->swt_handle,
                               frame->buffArray[i].buffer,
                               netc->swt_handle.cfg.userData);
#else
            _netc_rx_buff_free(&netc->ep_handle,
                               NETC_RING_ID,
                               frame->buffArray[i].buffer,
                               netc->ep_handle.cfg.userData);
#endif
        }
    }
}

static struct pbuf *_netc_rx_frame_to_pbufs(struct rt_imxrt_netc *netc, netc_frame_struct_t *frame)
{
    struct pbuf *p_root = RT_NULL;
    struct pbuf *p;
    rt_uint16_t i;

    for (i = 0; i < frame->length; i++)
    {
        netc_buffer_struct_t *buff = &frame->buffArray[i];
        netc_rx_pbuf_wrapper_t *rx_pbuf = _netc_find_rx_pbuf(buff->buffer);

        if (rx_pbuf == RT_NULL)
        {
            LOG_W("NETC Rx buffer is not in zero-copy pool");
            _netc_rx_release_frame_buffers(netc, frame, i);
            if (p_root != RT_NULL)
            {
                pbuf_free(p_root);
            }
            return RT_NULL;
        }

        p = pbuf_alloced_custom(PBUF_RAW,
                                buff->length,
                                PBUF_REF,
                                &rx_pbuf->custom,
                                buff->buffer,
                                NETC_RXBUFF_SIZE);
        if (p == RT_NULL)
        {
            LOG_W("pbuf_alloced_custom failed");
            _netc_rx_release_frame_buffers(netc, frame, i);
            if (p_root != RT_NULL)
            {
                pbuf_free(p_root);
            }
            return RT_NULL;
        }

        if (p_root == RT_NULL)
        {
            p_root = p;
        }
        else
        {
            pbuf_cat(p_root, p);
        }
    }

    return p_root;
}
#endif

static status_t _netc_tx_reclaim_callback(ep_handle_t *handle,
                                          uint8_t ring,
                                          netc_tx_frame_info_t *frameInfo,
                                          void *userData)
{
    RT_UNUSED(handle);
    RT_UNUSED(ring);
    RT_UNUSED(userData);

    if ((frameInfo != RT_NULL) && (frameInfo->context != RT_NULL))
    {
        rt_free(frameInfo->context);
        frameInfo->context = RT_NULL;
    }

    return kStatus_Success;
}

#if NETC_USE_SWT
static status_t _netc_swt_reclaim_callback(swt_handle_t *handle,
                                           netc_tx_frame_info_t *frameInfo,
                                           void *userData)
{
    RT_UNUSED(handle);
    RT_UNUSED(userData);

    if ((frameInfo != RT_NULL) && (frameInfo->context != RT_NULL))
    {
        rt_free(frameInfo->context);
        frameInfo->context = RT_NULL;
    }

    return kStatus_Success;
}

static status_t _netc_swt_add_table_entry(swt_handle_t *swt_handle)
{
    netc_tb_ipf_config_t ipf_entry_config;
    status_t result = kStatus_Success;
    uint32_t entry_id;
    rt_uint32_t i;

    rt_memset(&ipf_entry_config, 0, sizeof(ipf_entry_config));
    ipf_entry_config.keye.srcPortMask = NETC_SWT_USED_PORT_BITMAP;
    ipf_entry_config.cfge.hr = kNETC_SoftwareDefHR0;
    ipf_entry_config.cfge.fltfa = kNETC_IPFRedirectToMgmtPort;

    for (i = 0; i < NETC_SWT_MAX_PORT_NUM; i++)
    {
        if (((1U << i) & NETC_SWT_USED_PORT_BITMAP) == 0U)
        {
            continue;
        }

        entry_id = 0xFFFFFFFFU;
        ipf_entry_config.keye.srcPort = i;
        result = SWT_RxIPFAddTableEntry(swt_handle, &ipf_entry_config, &entry_id);
        if ((result != kStatus_Success) && (entry_id != 0xFFFFFFFFU))
        {
            LOG_E("SWT_RxIPFAddTableEntry failed: %d, entry %u", result, entry_id);
            return result;
        }
    }

    return result;
}

static rt_err_t _netc_swt_configure_link(struct rt_imxrt_netc *netc)
{
    status_t result;
    rt_uint32_t i;

    for (i = 0; i < NETC_SWT_MAX_PORT_NUM; i++)
    {
        if (((1U << i) & NETC_SWT_USED_PORT_BITMAP) == 0U)
        {
            continue;
        }

        result = SWT_SetEthPortMII(&netc->swt_handle, (uint8_t)i, NETC_LINK_SPEED, NETC_LINK_DUPLEX);
        if (result != kStatus_Success)
        {
            LOG_E("SWT_SetEthPortMII port %u failed: %d", i, result);
            return -RT_ERROR;
        }
    }

    return RT_EOK;
}

static rt_err_t _netc_swt_init(struct rt_imxrt_netc *netc)
{
    status_t result;
    rt_uint32_t i;

    if (netc->swt_inited)
    {
        return _netc_swt_configure_link(netc);
    }

    result = SWT_GetDefaultConfig(&g_netc_swt_config);
    if (result != kStatus_Success)
    {
        LOG_E("SWT_GetDefaultConfig failed: %d", result);
        return -RT_ERROR;
    }

    for (i = 0; i < NETC_SWT_MAX_PORT_NUM; i++)
    {
        if (((1U << i) & NETC_SWT_USED_PORT_BITMAP) == 0U)
        {
            continue;
        }

        g_netc_swt_config.ports[i].ethMac.miiMode = NETC_MII_MODE;
        g_netc_swt_config.ports[i].ethMac.miiSpeed = NETC_LINK_SPEED;
        g_netc_swt_config.ports[i].ethMac.miiDuplex = NETC_LINK_DUPLEX;
        g_netc_swt_config.ports[i].ethMac.rxMaxFrameSize = NETC_FRAME_MAX_FRAMELEN;
        g_netc_swt_config.ports[i].commonCfg.ipfCfg.enIPFTable = true;
    }

    g_netc_swt_config.bridgeCfg.dVFCfg.portMembership =
        (1U << NETC_SWT_PSEUDO_PORT) | NETC_SWT_USED_PORT_BITMAP;
    g_netc_swt_config.cmdRingUse = 1U;
    g_netc_swt_config.cmdBdrCfg[0].bdBase = g_netc_swt_cmd_bd;
    g_netc_swt_config.cmdBdrCfg[0].bdLength = NETC_CMDBD_NUM;

    result = SWT_Init(&netc->swt_handle, &g_netc_swt_config);
    if (result != kStatus_Success)
    {
        LOG_E("SWT_Init failed: %d", result);
        return -RT_ERROR;
    }

    rt_memset(&g_netc_swt_transfer_config, 0, sizeof(g_netc_swt_transfer_config));
    g_netc_swt_transfer_config.enUseMgmtRxBdRing = false;
    g_netc_swt_transfer_config.enUseMgmtTxBdRing = true;
    g_netc_swt_transfer_config.mgmtTxBdrConfig.bdArray = g_netc_tx_bd;
    g_netc_swt_transfer_config.mgmtTxBdrConfig.len = NETC_TXBD_NUM;
    g_netc_swt_transfer_config.mgmtTxBdrConfig.dirtyArray = g_netc_tx_dirty;
    g_netc_swt_transfer_config.mgmtTxBdrConfig.msixEntryIdx = TX_MSIX_ENTRY_IDX;
    g_netc_swt_transfer_config.mgmtTxBdrConfig.enIntr = true;
    g_netc_swt_transfer_config.reclaimCallback = _netc_swt_reclaim_callback;
#if NETC_RX_ZERO_COPY
    g_netc_swt_transfer_config.rxZeroCopy = true;
    g_netc_swt_transfer_config.rxBuffAlloc = _netc_rx_buff_alloc;
    g_netc_swt_transfer_config.rxBuffFree = _netc_rx_buff_free;
    g_netc_swt_transfer_config.userData = g_netc_rx_pbufs;
#else
    g_netc_swt_transfer_config.rxZeroCopy = false;
    g_netc_swt_transfer_config.rxBuffAlloc = RT_NULL;
    g_netc_swt_transfer_config.rxBuffFree = RT_NULL;
    g_netc_swt_transfer_config.userData = netc;
#endif
    g_netc_swt_transfer_config.rxCacheMaintain = true;
    g_netc_swt_transfer_config.txCacheMaintain = true;


    result = SWT_ManagementTxRxConfig(&netc->swt_handle, &netc->ep_handle, &g_netc_swt_transfer_config);
    if (result != kStatus_Success)
    {
        LOG_E("SWT_ManagementTxRxConfig failed: %d", result);
        return -RT_ERROR;
    }

    result = _netc_swt_add_table_entry(&netc->swt_handle);
    if (result != kStatus_Success)
    {
        return -RT_ERROR;
    }

    netc->swt_inited = RT_TRUE;

    return RT_EOK;
}
#endif

static rt_bool_t _netc_phy_get_link(struct rt_imxrt_netc *netc)
{
    uint16_t phy_status = 0;

    if (!netc->mdio_inited)
    {
        return RT_TRUE;
    }

    /* BMSR is latch-low on many PHYs, so read twice. */
    if (NETC_MDIORead(&netc->mdio_handle, NETC_PHY_ADDR, NETC_PHY_STATUS_REG, &phy_status) != kStatus_Success)
    {
        return netc->link_up;
    }

    if (NETC_MDIORead(&netc->mdio_handle, NETC_PHY_ADDR, NETC_PHY_STATUS_REG, &phy_status) != kStatus_Success)
    {
        return netc->link_up;
    }

    return ((phy_status & NETC_PHY_LINK_UP_MASK) != 0U) ? RT_TRUE : RT_FALSE;
}

static void _netc_msgintr_callback(MSGINTR_Type *base, uint8_t channel, uint32_t pendingIntr)
{
    RT_UNUSED(base);

    if (channel != 0)
    {
        return;
    }

    rt_interrupt_enter();

    if ((pendingIntr & (1U << TX_INTR_MSG_DATA)) != 0U)
    {
        EP_CleanTxIntrFlags(&imxrt_netc_device.ep_handle, 1, 0);
        rt_sem_release(&imxrt_netc_device.tx_isr_sem);
    }

    if ((pendingIntr & (1U << RX_INTR_MSG_DATA)) != 0U)
    {
        EP_CleanRxIntrFlags(&imxrt_netc_device.ep_handle, 1);
        (void)eth_device_ready(&imxrt_netc_device.parent);
    }

    rt_interrupt_leave();
}

static rt_err_t _netc_wait_tx_complete(struct rt_imxrt_netc *netc)
{
    if (rt_sem_take(&netc->tx_isr_sem, NETC_TX_TIMEOUT_TICK) != RT_EOK)
    {
        LOG_E("NETC Tx wait timeout");
        return -RT_ETIMEOUT;
    }

    rt_mutex_take(&netc->lock, RT_WAITING_FOREVER);
#if NETC_USE_SWT
#if defined(FSL_FEATURE_NETC_HAS_SWITCH_TAG) && FSL_FEATURE_NETC_HAS_SWITCH_TAG
    SWT_ReclaimTxDescriptor(&netc->swt_handle, NETC_RING_ID);
#else
    SWT_ReclaimTxDescriptor(&netc->swt_handle, false, NETC_RING_ID);
#endif
#else
    EP_ReclaimTxDescriptor(&netc->ep_handle, NETC_RING_ID);
#endif
    rt_mutex_release(&netc->lock);

    return RT_EOK;
}

static rt_err_t _netc_hw_init(struct rt_imxrt_netc *netc)
{
    ep_config_t ep_config;
    netc_bdr_config_t bdr_config;
    netc_rx_bdr_config_t rx_bdr_config[1];
    netc_tx_bdr_config_t tx_bdr_config[1];
    netc_mdio_config_t mdio_config;
    status_t result;
    rt_uint32_t i;

    if (netc->hw_inited)
    {
        return RT_EOK;
    }

    CLOCK_EnableClock(kCLOCK_Netc);

    /* RT1180 NETC access needs TRDC permissions and board-side NETC strap setup. */
    //BOARD_CommonSetting();
    PHY_Reset();
    BOARD_NETC_Init();

    for (i = 0; i < NETC_RXBD_NUM; i++)
    {
        g_netc_rx_buf_addr[i] = (uint64_t)(uintptr_t)&g_netc_rx_buf[i][0];
    }

#if NETC_RX_ZERO_COPY
    for (i = 0; i < NETC_RXBUFF_TOTAL_NUM; i++)
    {
        g_netc_rx_pbufs[i].custom.custom_free_function = _netc_rx_pbuf_free;
        g_netc_rx_pbufs[i].buffer = &g_netc_rx_buf[i][0];
#if NETC_USE_SWT
        g_netc_rx_pbufs[i].buffer_used = (i < NETC_RXBD_NUM) ? RT_TRUE : RT_FALSE;
        g_netc_rx_pbufs[i].handle = &netc->swt_handle;
#else
        g_netc_rx_pbufs[i].buffer_used = RT_FALSE;
        g_netc_rx_pbufs[i].handle = &netc->ep_handle;
#endif
        g_netc_rx_pbufs[i].ring = NETC_RING_ID;
    }
#endif

    rt_memset(g_netc_rx_bd, 0, sizeof(g_netc_rx_bd));
    rt_memset(g_netc_tx_bd, 0, sizeof(g_netc_tx_bd));
    rt_memset(g_netc_cmd_bd, 0, sizeof(g_netc_cmd_bd));
#if NETC_USE_SWT
    rt_memset(g_netc_swt_cmd_bd, 0, sizeof(g_netc_swt_cmd_bd));
#endif
    rt_memset(g_netc_tx_dirty, 0, sizeof(g_netc_tx_dirty));
    rt_memset(&ep_config, 0, sizeof(ep_config));
    rt_memset(&bdr_config, 0, sizeof(bdr_config));
    rt_memset(rx_bdr_config, 0, sizeof(rx_bdr_config));
    rt_memset(tx_bdr_config, 0, sizeof(tx_bdr_config));
    rt_memset(&mdio_config, 0, sizeof(mdio_config));

    /* MSIX and interrupt configuration. */
    MSGINTR_Init(NETC_MSGINTR, &_netc_msgintr_callback);
    uint32_t msgAddr = MSGINTR_GetIntrSelectAddr(NETC_MSGINTR, 0);
    g_netc_msix_entry[0].control = kNETC_MsixIntrMaskBit;
    g_netc_msix_entry[0].msgAddr = msgAddr;
    g_netc_msix_entry[0].msgData = TX_INTR_MSG_DATA;
    g_netc_msix_entry[1].control = kNETC_MsixIntrMaskBit;
    g_netc_msix_entry[1].msgAddr = msgAddr;
    g_netc_msix_entry[1].msgData = RX_INTR_MSG_DATA;

    result = EP_GetDefaultConfig(&ep_config);
    if (result != kStatus_Success)
    {
        LOG_E("EP_GetDefaultConfig failed: %d", result);
        return -RT_ERROR;
    }

    ep_config.si = NETC_PSI;
    ep_config.siConfig.rxRingUse = 1U;
    ep_config.siConfig.txRingUse = 1U;
    ep_config.siConfig.rxBdrGroupNum = 0U;
    ep_config.siConfig.ringPerBdrGroup = 1U;
    ep_config.siConfig.defaultRxBdrGroup = kNETC_SiBDRGroupOne;
    ep_config.entryNum = 2U;
    ep_config.msixEntry = &g_netc_msix_entry[0];
    ep_config.cmdBdEntryIdx = 0U;
    ep_config.siComEntryIdx = 0U;
    ep_config.timerSyncEntryIdx = 0U;
    ep_config.reclaimCallback = _netc_tx_reclaim_callback;
#if NETC_RX_ZERO_COPY
    ep_config.userData = g_netc_rx_pbufs;
#else
    ep_config.userData = netc;
#endif
    ep_config.rxCacheMaintain = false;
#if defined(FSL_ETH_ENABLE_CACHE_CONTROL)
    ep_config.txCacheMaintain = true;
#else
    ep_config.txCacheMaintain = false;
#endif
#if NETC_RX_ZERO_COPY && !NETC_USE_SWT
    ep_config.rxZeroCopy = true;
    ep_config.rxBuffAlloc = _netc_rx_buff_alloc;
    ep_config.rxBuffFree = _netc_rx_buff_free;
#else
    ep_config.rxZeroCopy = false;
    ep_config.rxBuffAlloc = RT_NULL;
    ep_config.rxBuffFree = RT_NULL;
#endif
    ep_config.cmdBdrConfig.bdBase = g_netc_cmd_bd;
    ep_config.cmdBdrConfig.bdLength = NETC_CMDBD_NUM;
    ep_config.cmdBdrConfig.enCompInt = false;

    ep_config.port.ethMac.miiMode = NETC_MII_MODE;
    ep_config.port.ethMac.miiSpeed = NETC_LINK_SPEED;
    ep_config.port.ethMac.miiDuplex = NETC_LINK_DUPLEX;
    ep_config.port.ethMac.rxMaxFrameSize = NETC_FRAME_MAX_FRAMELEN;

    rx_bdr_config[0].extendDescEn = false;
    rx_bdr_config[0].bdArray = g_netc_rx_bd;
    rx_bdr_config[0].len = NETC_RXBD_NUM;
    rx_bdr_config[0].buffAddrArray = g_netc_rx_buf_addr;
    rx_bdr_config[0].buffSize = NETC_RXBUFF_SIZE;
    rx_bdr_config[0].enThresIntr = true;
    rx_bdr_config[0].enCoalIntr = true;
    rx_bdr_config[0].intrThreshold = 1U;
    rx_bdr_config[0].intrTimerThres = 0U;
    rx_bdr_config[0].msixEntryIdx = RX_MSIX_ENTRY_IDX;
    rx_bdr_config[0].disVlanPresent = false;
    rx_bdr_config[0].enVlanExtract = false;
    rx_bdr_config[0].isKeepCRC = false;
    rx_bdr_config[0].congestionMode = false;
    rx_bdr_config[0].enHeaderAlign = false;

    tx_bdr_config[0].len = NETC_TXBD_NUM;
    tx_bdr_config[0].bdArray = g_netc_tx_bd;
    tx_bdr_config[0].dirtyArray = g_netc_tx_dirty;
    tx_bdr_config[0].enIntr = true;
    tx_bdr_config[0].enThresIntr = false;
    tx_bdr_config[0].enCoalIntr = false;
    tx_bdr_config[0].intrThreshold = 0U;
    tx_bdr_config[0].intrTimerThres = 0U;
    tx_bdr_config[0].msixEntryIdx = TX_MSIX_ENTRY_IDX;
    tx_bdr_config[0].isVlanInsert = false;
    tx_bdr_config[0].isUserCRC = false;
    tx_bdr_config[0].wrrWeight = 1U;
    tx_bdr_config[0].priority = 0U;

    bdr_config.rxBdrConfig = rx_bdr_config;
    bdr_config.txBdrConfig = tx_bdr_config;

    result = EP_Init(&netc->ep_handle, netc->dev_addr, &ep_config, &bdr_config);
    if (result != kStatus_Success)
    {
        LOG_E("EP_Init failed: %d", result);
        return -RT_ERROR;
    }

    mdio_config.mdio.type = kNETC_EMdio;
    mdio_config.srcClockHz = CLOCK_GetRootClockFreq(kCLOCK_Root_Netc);
    mdio_config.isNegativeDriven = false;
    mdio_config.isPreambleDisable = false;

    result = NETC_MDIOInit(&netc->mdio_handle, &mdio_config);
    if (result == kStatus_Success)
    {
        netc->mdio_inited = RT_TRUE;
    }
    else
    {
        netc->mdio_inited = RT_FALSE;
        LOG_W("NETC_MDIOInit failed: %d, continue with fixed MAC configuration", result);
    }

    result = EP_Up(&netc->ep_handle, NETC_LINK_SPEED, NETC_LINK_DUPLEX);
    if (result != kStatus_Success)
    {
        LOG_E("EP_Up failed: %d", result);
        return -RT_ERROR;
    }

#if NETC_USE_SWT
    if (_netc_swt_init(netc) != RT_EOK)
    {
        return -RT_ERROR;
    }
#endif

    netc->link_up = _netc_phy_get_link(netc);
    if (!netc->link_up)
    {
#if !NETC_USE_SWT
        (void)EP_Down(&netc->ep_handle);
#endif
    }

    netc->hw_inited = RT_TRUE;

    EP_MsixSetEntryMask(&netc->ep_handle, TX_MSIX_ENTRY_IDX, false);
    EP_MsixSetEntryMask(&netc->ep_handle, RX_MSIX_ENTRY_IDX, false);

#if NETC_USE_SWT
    LOG_I("NETC ENETC1 initialized, MAC %02x:%02x:%02x:%02x:%02x:%02x",
          netc->dev_addr[0], netc->dev_addr[1], netc->dev_addr[2],
          netc->dev_addr[3], netc->dev_addr[4], netc->dev_addr[5]);
#else
    LOG_I("NETC ENETC0 initialized, MAC %02x:%02x:%02x:%02x:%02x:%02x",
          netc->dev_addr[0], netc->dev_addr[1], netc->dev_addr[2],
          netc->dev_addr[3], netc->dev_addr[4], netc->dev_addr[5]);
#endif

    return RT_EOK;
}

static void _netc_poll_thread_entry(void *parameter)
{
    struct rt_imxrt_netc *netc = (struct rt_imxrt_netc *)parameter;

    while (1)
    {
        rt_bool_t link_now;

        if (!netc->hw_inited)
        {
            rt_thread_delay(NETC_PHY_POLL_DELAY_TICK);
            continue;
        }

        link_now = _netc_phy_get_link(netc);
        if (link_now != netc->link_up)
        {
            rt_mutex_take(&netc->lock, RT_WAITING_FOREVER);
            if (link_now)
            {
                (void)EP_Up(&netc->ep_handle, NETC_LINK_SPEED, NETC_LINK_DUPLEX);
#if NETC_USE_SWT
                (void)_netc_swt_init(netc);
#endif
            }
            else
            {
#if !NETC_USE_SWT
                (void)EP_Down(&netc->ep_handle);
#endif
            }
            netc->link_up = link_now;
            rt_mutex_release(&netc->lock);

            eth_device_linkchange(&netc->parent, netc->link_up);
        }

        rt_thread_delay(NETC_PHY_POLL_DELAY_TICK);
    }
}

static rt_err_t rt_imxrt_netc_init(rt_device_t dev)
{
    RT_UNUSED(dev);
    return _netc_hw_init(&imxrt_netc_device);
}

static rt_err_t rt_imxrt_netc_open(rt_device_t dev, rt_uint16_t oflag)
{
    RT_UNUSED(dev);
    RT_UNUSED(oflag);
    return RT_EOK;
}

static rt_err_t rt_imxrt_netc_close(rt_device_t dev)
{
    RT_UNUSED(dev);
    return RT_EOK;
}

static rt_ssize_t rt_imxrt_netc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    RT_UNUSED(dev);
    RT_UNUSED(pos);
    RT_UNUSED(buffer);
    RT_UNUSED(size);
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

static rt_ssize_t rt_imxrt_netc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    RT_UNUSED(dev);
    RT_UNUSED(pos);
    RT_UNUSED(buffer);
    RT_UNUSED(size);
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

static rt_err_t rt_imxrt_netc_control(rt_device_t dev, int cmd, void *args)
{
    RT_UNUSED(dev);

    switch (cmd)
    {
    case NIOCTL_GADDR:
        if (args == RT_NULL)
        {
            return -RT_ERROR;
        }

        rt_memcpy(args, imxrt_netc_device.dev_addr, sizeof(imxrt_netc_device.dev_addr));
        break;

    default:
        break;
    }

    return RT_EOK;
}

static rt_err_t rt_imxrt_netc_tx(rt_device_t dev, struct pbuf *p)
{
    struct rt_imxrt_netc *netc = &imxrt_netc_device;
    netc_buffer_struct_t tx_buff;
    netc_frame_struct_t frame;
#if NETC_USE_SWT
    swt_tx_opt tx_opt;
#else
    ep_tx_opt tx_opt;
#endif
    void *tx_data;
    status_t result;

    RT_UNUSED(dev);
    RT_ASSERT(p != RT_NULL);

    if (!netc->hw_inited)
    {
        return -RT_ERROR;
    }

    if (!netc->link_up)
    {
        return -RT_ERROR;
    }

    if (p->tot_len == 0U)
    {
        return -RT_ERROR;
    }

    while (rt_sem_take(&netc->tx_isr_sem, 0) == RT_EOK)
    {
    }

    tx_data = rt_malloc(p->tot_len);
    if (tx_data == RT_NULL)
    {
        return -RT_ENOMEM;
    }

    (void)pbuf_copy_partial(p, tx_data, p->tot_len, 0);

    tx_buff.buffer = tx_data;
    tx_buff.length = (uint16_t)p->tot_len;
    frame.buffArray = &tx_buff;
    frame.length = 1U;
    rt_memset(&tx_opt, 0, sizeof(tx_opt));

    do
    {
        rt_mutex_take(&netc->lock, RT_WAITING_FOREVER);
#if NETC_USE_SWT
#if defined(FSL_FEATURE_NETC_HAS_SWITCH_TAG) && FSL_FEATURE_NETC_HAS_SWITCH_TAG
        result = SWT_SendFrame(&netc->swt_handle, &frame, tx_data, &tx_opt);
#else
        {
            swt_mgmt_tx_arg_t tx_arg = { .ring = NETC_RING_ID };

            result = SWT_SendFrame(&netc->swt_handle,
                                   tx_arg,
                                   NETC_SWT_TX_PORT,
                                   false,
                                   &frame,
                                   tx_data,
                                   &tx_opt);
        }
#endif
#else
        result = EP_SendFrame(&netc->ep_handle, NETC_RING_ID, &frame, tx_data, &tx_opt);
#endif
        rt_mutex_release(&netc->lock);

        if (result == kStatus_Busy)
        {
            rt_thread_mdelay(1);
        }
    } while (result == kStatus_Busy);

    if (result != kStatus_Success)
    {
        rt_free(tx_data);
#if NETC_USE_SWT
        LOG_E("SWT_SendFrame failed: %d", result);
#else
        LOG_E("EP_SendFrame failed: %d", result);
#endif
        return -RT_ERROR;
    }
    if (result == kStatus_Success)
    {
        return _netc_wait_tx_complete(netc);
    }

    return RT_EOK;
}

static struct pbuf *rt_imxrt_netc_rx(rt_device_t dev)
{
    struct rt_imxrt_netc *netc = &imxrt_netc_device;
    struct pbuf *p = RT_NULL;
#if NETC_RX_ZERO_COPY
    netc_buffer_struct_t rx_buffers[NETC_MAX_BUFFERS_PER_FRAME];
    netc_frame_struct_t frame;
    status_t result;
#else
    uint32_t length = 0U;
    status_t result;
#endif

    RT_UNUSED(dev);

    if (!netc->hw_inited)
    {
        return RT_NULL;
    }

    rt_mutex_take(&netc->lock, RT_WAITING_FOREVER);

#if NETC_RX_ZERO_COPY
    frame.buffArray = rx_buffers;
    frame.length = NETC_MAX_BUFFERS_PER_FRAME;

#if NETC_USE_SWT
    result = SWT_ReceiveFrame(&netc->swt_handle, &frame, RT_NULL);
#else
    result = EP_ReceiveFrame(&netc->ep_handle, NETC_RING_ID, &frame, RT_NULL);
#endif
    if (result == kStatus_Success)
    {
        p = _netc_rx_frame_to_pbufs(netc, &frame);
    }
#if NETC_USE_SWT
    else if ((result == kStatus_NETC_RxFrameError) ||
             (result == kStatus_NETC_RxTsrResp))
    {
        (void)SWT_ReceiveFrameCopy(&netc->swt_handle, RT_NULL, 0U, RT_NULL);
    }
    else if ((result != kStatus_NETC_RxFrameEmpty) &&
             (result != kStatus_NETC_LackOfResource) &&
             (result != kStatus_NETC_RxHRZeroFrame))
    {
        LOG_W("SWT_ReceiveFrame failed: %d", result);
    }
#else
    else if ((result == kStatus_NETC_RxHRNotZeroFrame) ||
             (result == kStatus_NETC_RxTsrResp))
    {
        EP_DropFrame(&netc->ep_handle, &netc->ep_handle.rxBdRing[NETC_RING_ID], NETC_RING_ID);
    }
    else if ((result != kStatus_NETC_RxFrameEmpty) &&
             (result != kStatus_NETC_LackOfResource) &&
             (result != kStatus_NETC_RxFrameError))
    {
        LOG_W("EP_ReceiveFrame failed: %d", result);
    }
#endif
#else
#if NETC_USE_SWT
    result = SWT_GetRxFrameSize(&netc->swt_handle, &length);
#else
    result = EP_GetRxFrameSize(&netc->ep_handle, NETC_RING_ID, &length);
#endif
    if ((result == kStatus_Success) && (length != 0U))
    {
        p = pbuf_alloc(PBUF_RAW, (u16_t)length, PBUF_RAM);
        if (p == RT_NULL)
        {
#if NETC_USE_SWT
            (void)SWT_ReceiveFrameCopy(&netc->swt_handle, RT_NULL, 0U, RT_NULL);
#else
            (void)EP_ReceiveFrameCopy(&netc->ep_handle, NETC_RING_ID, RT_NULL, 0U, RT_NULL);
#endif
            rt_mutex_release(&netc->lock);
            return RT_NULL;
        }

#if NETC_USE_SWT
        result = SWT_ReceiveFrameCopy(&netc->swt_handle, p->payload, length, RT_NULL);
#else
        result = EP_ReceiveFrameCopy(&netc->ep_handle, NETC_RING_ID, p->payload, length, RT_NULL);
#endif
        if (result != kStatus_Success)
        {
            pbuf_free(p);
            p = RT_NULL;
#if NETC_USE_SWT
            LOG_W("SWT_ReceiveFrameCopy failed: %d", result);
#else
            LOG_W("EP_ReceiveFrameCopy failed: %d", result);
#endif
        }
    }
#if NETC_USE_SWT
    else if ((result == kStatus_NETC_RxFrameError) ||
             (result == kStatus_NETC_RxTsrResp))
    {
        (void)SWT_ReceiveFrameCopy(&netc->swt_handle, RT_NULL, 0U, RT_NULL);
    }
#else
    else if ((result == kStatus_NETC_RxFrameError) ||
             (result == kStatus_NETC_RxHRNotZeroFrame) ||
             (result == kStatus_NETC_RxTsrResp))
    {
        (void)EP_ReceiveFrameCopy(&netc->ep_handle, NETC_RING_ID, RT_NULL, 0U, RT_NULL);
    }
#endif
#endif

    rt_mutex_release(&netc->lock);

    return p;
}

static int rt_hw_imxrt_netc_init(void)
{
    rt_err_t state;
    rt_thread_t tid;

    imxrt_netc_device.dev_addr[0] = NETC_DEFAULT_MAC0;
    imxrt_netc_device.dev_addr[1] = NETC_DEFAULT_MAC1;
    imxrt_netc_device.dev_addr[2] = NETC_DEFAULT_MAC2;
    imxrt_netc_device.dev_addr[3] = NETC_DEFAULT_MAC3;
    imxrt_netc_device.dev_addr[4] = NETC_DEFAULT_MAC4;
    imxrt_netc_device.dev_addr[5] = NETC_DEFAULT_MAC5;

    rt_mutex_init(&imxrt_netc_device.lock, "netc", RT_IPC_FLAG_FIFO);
    rt_sem_init(&imxrt_netc_device.tx_isr_sem, "ntx", 0, RT_IPC_FLAG_FIFO);

    imxrt_netc_device.parent.parent.init = rt_imxrt_netc_init;
    imxrt_netc_device.parent.parent.open = rt_imxrt_netc_open;
    imxrt_netc_device.parent.parent.close = rt_imxrt_netc_close;
    imxrt_netc_device.parent.parent.read = rt_imxrt_netc_read;
    imxrt_netc_device.parent.parent.write = rt_imxrt_netc_write;
    imxrt_netc_device.parent.parent.control = rt_imxrt_netc_control;
    imxrt_netc_device.parent.parent.user_data = &imxrt_netc_device;

    imxrt_netc_device.parent.eth_rx = rt_imxrt_netc_rx;
    imxrt_netc_device.parent.eth_tx = rt_imxrt_netc_tx;

    state = _netc_hw_init(&imxrt_netc_device);
    if (state != RT_EOK)
    {
        return state;
    }

    state = eth_device_init(&imxrt_netc_device.parent, "e0");
    if (state != RT_EOK)
    {
        LOG_E("eth_device_init failed: %d", state);
        return state;
    }

    eth_device_linkchange(&imxrt_netc_device.parent, imxrt_netc_device.link_up);

    tid = rt_thread_create("netc",
                           _netc_poll_thread_entry,
                           &imxrt_netc_device,
                           RT_NTCP_THREAD_STACK_SIZE,
                           RT_NTCP_THREAD_PRIORITY,
                           10);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        LOG_W("failed to create NETC poll thread");
    }

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_imxrt_netc_init);

#endif /* RT_USING_LWIP */