summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/rt_drivers/drv_emac.c
blob: f7d93f2719e4f20059e73b478c626e374e8084bd (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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-05-16     shelton      first version
 * 2022-07-11     shelton      optimize code to improve network throughput
 *                             performance
 * 2022-10-15     shelton      optimize code
 * 2023-10-18     shelton      optimize code
 * 2024-09-02     shelton      add support phy lan8720 and yt8512
 * 2024-12-18     shelton      add support f457
 */

#include "drv_emac.h"
#include <netif/ethernetif.h>
#include <lwipopts.h>

/* debug option */
//#define EMAC_RX_DUMP
//#define EMAC_TX_DUMP
//#define DRV_DEBUG
#define LOG_TAG                         "drv.emac"
#include <drv_log.h>

#define CRYSTAL_ON_PHY                  0

/* emac memory buffer configuration */
#define EMAC_NUM_RX_BUF                 5    /* rx (5 * 1500) */
#define EMAC_NUM_TX_BUF                 5    /* tx (5 * 1500) */

#define MAX_ADDR_LEN                    6

#define DMARXDESC_FRAMELENGTH_SHIFT     16

struct rt_at32_emac
{
    /* inherit from ethernet device */
    struct eth_device parent;
#ifndef PHY_USING_INTERRUPT_MODE
    rt_timer_t poll_link_timer;
#endif

    /* interface address info, hw address */
    rt_uint8_t dev_addr[MAX_ADDR_LEN];
    /* emac_speed */
    emac_speed_type emac_speed;
    /* emac_duplex_mode */
    emac_duplex_type emac_mode;
};

typedef struct {
    rt_uint32_t length;
    rt_uint32_t buffer;
    emac_dma_desc_type *descriptor;
    emac_dma_desc_type *rx_fs_desc;
    emac_dma_desc_type *rx_ls_desc;
    rt_uint8_t g_seg_count;
} frame_type;

static emac_dma_desc_type *dma_rx_dscr_tab, *dma_tx_dscr_tab;
extern emac_dma_desc_type *dma_rx_desc_to_get, *dma_tx_desc_to_set;

frame_type rx_frame;

static rt_uint8_t *rx_buff, *tx_buff;
static struct rt_at32_emac at32_emac_device;
static uint8_t phy_addr = 0xFF;

#if defined(EMAC_RX_DUMP) || defined(EMAC_TX_DUMP)
#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
static void dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
{
    unsigned char *buf = (unsigned char *)ptr;
    int i, j;

    for (i = 0; i < buflen; i += 16)
    {
        rt_kprintf("%08X: ", i);

        for (j = 0; j < 16; j++)
            if (i + j < buflen)
                rt_kprintf("%02X ", buf[i + j]);
            else
                rt_kprintf("   ");
        rt_kprintf(" ");

        for (j = 0; j < 16; j++)
            if (i + j < buflen)
                rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
        rt_kprintf("\n");
    }
}
#endif

/**
  * @brief phy reset
  */
static void phy_reset(void)
{
    gpio_init_type gpio_init_struct;

#if defined (SOC_SERIES_AT32F437) || defined (SOC_SERIES_AT32F457)
    crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_GPIOG_PERIPH_CLOCK, TRUE);

    gpio_default_para_init(&gpio_init_struct);
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init_struct.gpio_pins = GPIO_PINS_15;
    gpio_init(GPIOE, &gpio_init_struct);

    gpio_init_struct.gpio_pins = GPIO_PINS_15;
    gpio_init(GPIOG, &gpio_init_struct);

    gpio_bits_reset(GPIOE, GPIO_PINS_15);
    gpio_bits_reset(GPIOG, GPIO_PINS_15);
    rt_thread_mdelay(2);
    gpio_bits_set(GPIOE, GPIO_PINS_15);
#endif
#if defined (SOC_SERIES_AT32F407)
    crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);

    gpio_default_para_init(&gpio_init_struct);
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init_struct.gpio_pins = GPIO_PINS_8;
    gpio_init(GPIOC, &gpio_init_struct);

    gpio_bits_reset(GPIOC, GPIO_PINS_8);
    rt_thread_mdelay(2);
    gpio_bits_set(GPIOC, GPIO_PINS_8);
#endif
    rt_thread_mdelay(2000);
}

/**
  * @brief phy clock config
  */
static void phy_clock_config(void)
{
#if (CRYSTAL_ON_PHY == 0)
    /* if CRYSTAL_NO_PHY, output clock with pa8 of mcu */
    gpio_init_type gpio_init_struct;

    crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);

    gpio_default_para_init(&gpio_init_struct);
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init_struct.gpio_pins = GPIO_PINS_8;
    gpio_init(GPIOA, &gpio_init_struct);

    /* 9162 clkout output 25 mhz */
    /* 83848 clkout output 50 mhz */
#if defined (SOC_SERIES_AT32F407)
    crm_clock_out_set(CRM_CLKOUT_SCLK);
#if defined (PHY_USING_DM9162) || defined (PHY_USING_LAN8720) || \
    defined (PHY_USING_YT8512)
    crm_clkout_div_set(CRM_CLKOUT_DIV_8);
#elif defined (PHY_USING_DP83848)
    crm_clkout_div_set(CRM_CLKOUT_DIV_4);
#endif
#endif

#if defined (SOC_SERIES_AT32F437)
    crm_clock_out1_set(CRM_CLKOUT1_PLL);
#if defined (PHY_USING_DM9162) || defined (PHY_USING_LAN8720) || \
    defined (PHY_USING_YT8512)
    crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_5, CRM_CLKOUT_DIV2_2);
#elif defined (PHY_USING_DP83848)
    crm_clkout_div_set(CRM_CLKOUT_INDEX_1, CRM_CLKOUT_DIV1_5, CRM_CLKOUT_DIV2_1);
#endif
#endif
#endif
}

/**
  * @brief reset phy register
  */
static error_status emac_phy_register_reset(void)
{
    uint16_t data = 0;
    uint32_t timeout = 0;
    uint32_t i = 0;

    if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, PHY_RESET_BIT) == ERROR)
    {
        return ERROR;
    }

    for(i = 0; i < 0x000FFFFF; i++);

    do
    {
        timeout++;
        if(emac_phy_register_read(phy_addr, PHY_CONTROL_REG, &data) == ERROR)
        {
            return ERROR;
        }
    } while((data & PHY_RESET_BIT) && (timeout < PHY_TIMEOUT));

    for(i = 0; i < 0x00FFFFF; i++);
    if(timeout == PHY_TIMEOUT)
    {
        return ERROR;
    }
    return SUCCESS;
}

/**
  * @brief set mac speed related parameters
  */
static error_status emac_speed_config(emac_auto_negotiation_type nego, emac_duplex_type mode, emac_speed_type speed)
{
    uint16_t data = 0;
    uint32_t timeout = 0;
    if(nego == EMAC_AUTO_NEGOTIATION_ON)
    {
        do
        {
            timeout++;
            if(emac_phy_register_read(phy_addr, PHY_STATUS_REG, &data) == ERROR)
            {
              return ERROR;
            }
        } while(!(data & PHY_LINKED_STATUS_BIT) && (timeout < PHY_TIMEOUT));

        if(timeout == PHY_TIMEOUT)
        {
            return ERROR;
        }

        timeout = 0;

        if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, PHY_AUTO_NEGOTIATION_BIT) == ERROR)
        {
            return ERROR;
        }

        do
        {
            timeout++;
            if(emac_phy_register_read(phy_addr, PHY_STATUS_REG, &data) == ERROR)
            {
                return ERROR;
            }
        } while(!(data & PHY_NEGO_COMPLETE_BIT) && (timeout < PHY_TIMEOUT));

        if(timeout == PHY_TIMEOUT)
        {
            return ERROR;
        }

        if(emac_phy_register_read(phy_addr, PHY_SPECIFIED_CS_REG, &data) == ERROR)
        {
            return ERROR;
        }
#if defined (PHY_USING_DM9162) || defined (PHY_USING_LAN8720)
        if(data & PHY_FULL_DUPLEX_100MBPS_BIT)
        {
            emac_fast_speed_set(EMAC_SPEED_100MBPS);
            emac_duplex_mode_set(EMAC_FULL_DUPLEX);
        }
        else if(data & PHY_HALF_DUPLEX_100MBPS_BIT)
        {
            emac_fast_speed_set(EMAC_SPEED_100MBPS);
            emac_duplex_mode_set(EMAC_HALF_DUPLEX);
        }
        else if(data & PHY_FULL_DUPLEX_10MBPS_BIT)
        {
            emac_fast_speed_set(EMAC_SPEED_10MBPS);
            emac_duplex_mode_set(EMAC_FULL_DUPLEX);
        }
        else if(data & PHY_HALF_DUPLEX_10MBPS_BIT)
        {
            emac_fast_speed_set(EMAC_SPEED_10MBPS);
            emac_duplex_mode_set(EMAC_HALF_DUPLEX);
        }
#endif
#if defined (PHY_USING_DP83848)
        if(data & PHY_DUPLEX_MODE)
        {
            emac_duplex_mode_set(EMAC_FULL_DUPLEX);
        }
        else
        {
            emac_duplex_mode_set(EMAC_HALF_DUPLEX);
        }
        if(data & PHY_SPEED_MODE)
        {
            emac_fast_speed_set(EMAC_SPEED_10MBPS);
        }
        else
        {
            emac_fast_speed_set(EMAC_SPEED_100MBPS);
        }
#endif
#if defined (PHY_USING_YT8512)
        if(data & PHY_DUPLEX_MODE)
        {
            emac_duplex_mode_set(EMAC_FULL_DUPLEX);
        }
        else
        {
            emac_duplex_mode_set(EMAC_HALF_DUPLEX);
        }
        if(data & PHY_SPEED_MODE)
        {
            emac_fast_speed_set(EMAC_SPEED_100MBPS);
        }
        else
        {
            emac_fast_speed_set(EMAC_SPEED_10MBPS);
        }
#endif
    }
    else
    {
        if(emac_phy_register_write(phy_addr, PHY_CONTROL_REG, (uint16_t)((mode << 8) | (speed << 13))) == ERROR)
        {
            return ERROR;
        }
        if(speed == EMAC_SPEED_100MBPS)
        {
            emac_fast_speed_set(EMAC_SPEED_100MBPS);
        }
        else
        {
            emac_fast_speed_set(EMAC_SPEED_10MBPS);
        }
        if(mode == EMAC_FULL_DUPLEX)
        {
            emac_duplex_mode_set(EMAC_FULL_DUPLEX);
        }
        else
        {
            emac_duplex_mode_set(EMAC_HALF_DUPLEX);
        }
    }

    return SUCCESS;
}

/**
  * @brief initialize emac phy
  */
static error_status emac_phy_init(emac_control_config_type *control_para)
{
    emac_clock_range_set();
    if(emac_phy_register_reset() == ERROR)
    {
        return ERROR;
    }
    if(emac_speed_config(control_para->auto_nego, control_para->duplex_mode, control_para->fast_ethernet_speed) == ERROR)
    {
        return ERROR;
    }

    emac_control_config(control_para);
    return SUCCESS;
}

/**
  * @brief emac initialization function
  */
static rt_err_t rt_at32_emac_init(rt_device_t dev)
{
    emac_control_config_type mac_control_para;
    emac_dma_config_type dma_control_para;

    /* check till phy detected */
    while(phy_addr == 0xFF)
    {
        rt_thread_mdelay(1000);
    }

    /* emac reset */
    emac_reset();

    /* software reset emac dma */
    emac_dma_software_reset_set();
    while(emac_dma_software_reset_get() == SET);

    emac_control_para_init(&mac_control_para);
    mac_control_para.auto_nego = EMAC_AUTO_NEGOTIATION_ON;

    if(emac_phy_init(&mac_control_para) == ERROR)
    {
        LOG_E("emac hardware init failed");
        return -RT_ERROR;
    }
    else
    {
        LOG_D("emac hardware init success");
    }

    emac_transmit_flow_control_enable(TRUE);
    emac_zero_quanta_pause_disable(TRUE);

    /* set mac address */
    emac_local_address_set(at32_emac_device.dev_addr);

    /* set emac dma rx link list */
    emac_dma_descriptor_list_address_set(EMAC_DMA_RECEIVE, dma_rx_dscr_tab, rx_buff, EMAC_NUM_RX_BUF);
    /* set emac dma tx link list */
    emac_dma_descriptor_list_address_set(EMAC_DMA_TRANSMIT, dma_tx_dscr_tab, tx_buff, EMAC_NUM_TX_BUF);

    emac_dma_para_init(&dma_control_para);
    dma_control_para.rsf_enable = TRUE;
    dma_control_para.tsf_enable = TRUE;
    dma_control_para.osf_enable = TRUE;
    dma_control_para.aab_enable = TRUE;
    dma_control_para.usp_enable = TRUE;
    dma_control_para.fb_enable = TRUE;
    dma_control_para.flush_rx_disable = TRUE;
    dma_control_para.rx_dma_pal = EMAC_DMA_PBL_32;
    dma_control_para.tx_dma_pal = EMAC_DMA_PBL_32;
    dma_control_para.priority_ratio = EMAC_DMA_2_RX_1_TX;
    emac_dma_config(&dma_control_para);
    /* emac interrupt init */
    emac_dma_interrupt_enable(EMAC_DMA_INTERRUPT_NORMAL_SUMMARY, TRUE);
    emac_dma_interrupt_enable(EMAC_DMA_INTERRUPT_RX, TRUE);
    nvic_irq_enable(EMAC_IRQn, 0x07, 0);

    /* enable emac */
    emac_start();

    return RT_EOK;
}

static rt_err_t rt_at32_emac_open(rt_device_t dev, rt_uint16_t oflag)
{
    LOG_D("emac open");
    return RT_EOK;
}

static rt_err_t rt_at32_emac_close(rt_device_t dev)
{
    LOG_D("emac close");
    return RT_EOK;
}

static rt_ssize_t rt_at32_emac_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    LOG_D("emac read");
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

static rt_ssize_t rt_at32_emac_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
    LOG_D("emac write");
    rt_set_errno(-RT_ENOSYS);
    return 0;
}

static rt_err_t rt_at32_emac_control(rt_device_t dev, int cmd, void *args)
{
    switch (cmd)
    {
    case NIOCTL_GADDR:
        /* get mac address */
        if (args)
        {
            SMEMCPY(args, at32_emac_device.dev_addr, 6);
        }
        else
        {
            return -RT_ERROR;
        }
        break;

    default :
        break;
    }

    return RT_EOK;
}

/**
  * @brief emac txpkt chainmode
  */
rt_err_t emac_txpkt_chainmode(rt_uint32_t frame_length)
{
    rt_uint32_t buf_cnt = 0, index = 0;

    /* check if the descriptor is owned by the ethernet dma (when set) or cpu (when reset) */
    if((dma_tx_desc_to_set->status & EMAC_DMATXDESC_OWN) != (u32)RESET)
    {
        /* return error: own bit set */
        return -RT_ERROR;
    }

    if(frame_length == 0)
    {
        return -RT_ERROR;
    }

    if(frame_length > EMAC_MAX_PACKET_LENGTH)
    {
        buf_cnt = frame_length / EMAC_MAX_PACKET_LENGTH;
        if(frame_length % EMAC_MAX_PACKET_LENGTH)
        {
          buf_cnt += 1;
        }
    }
    else
    {
        buf_cnt = 1;
    }

    if(buf_cnt == 1)
    {
        /* setting the last segment and first segment bits (in this case a frame is transmitted in one descriptor) */
        dma_tx_desc_to_set->status |= EMAC_DMATXDESC_LS | EMAC_DMATXDESC_FS;

        /* setting the frame length: bits[12:0] */
        dma_tx_desc_to_set->controlsize = (frame_length & EMAC_DMATXDESC_TBS1);

        /* set own bit of the tx descriptor status: gives the buffer back to ethernet dma */
        dma_tx_desc_to_set->status |= EMAC_DMATXDESC_OWN;

        /* selects the next dma tx descriptor list for next buffer to send */
        dma_tx_desc_to_set = (emac_dma_desc_type*) (dma_tx_desc_to_set->buf2nextdescaddr);
    }
    else
    {
        for(index = 0; index < buf_cnt; index ++)
        {
            /* clear first and last segments */
            dma_tx_desc_to_set->status &= ~(EMAC_DMATXDESC_LS | EMAC_DMATXDESC_FS);

            /* set first segments */
            if(index == 0)
            {
                dma_tx_desc_to_set->status |= EMAC_DMATXDESC_FS;
            }

            /* set size */
            dma_tx_desc_to_set->controlsize = (EMAC_MAX_PACKET_LENGTH & EMAC_DMATXDESC_TBS1);

            /* set last segments */
            if(index == (buf_cnt - 1))
            {
                dma_tx_desc_to_set->status |= EMAC_DMATXDESC_LS;
                dma_tx_desc_to_set->controlsize = ((frame_length - ((buf_cnt-1) * EMAC_MAX_PACKET_LENGTH)) & EMAC_DMATXDESC_TBS1);
            }

            /* set own bit of the tx descriptor status: gives the buffer back to ethernet dma */
            dma_tx_desc_to_set->status |= EMAC_DMATXDESC_OWN;

            /* selects the next dma tx descriptor list for next buffer to send */
            dma_tx_desc_to_set = (emac_dma_desc_type*) (dma_tx_desc_to_set->buf2nextdescaddr);
        }
    }

    /* when tx buffer unavailable flag is set: clear it and resume transmission */
    if(emac_dma_flag_get(EMAC_DMA_TBU_FLAG))
    {
        /* clear tbus ethernet dma flag */
        emac_dma_flag_clear(EMAC_DMA_TBU_FLAG);
        /* resume dma transmission*/
        EMAC_DMA->tpd_bit.tpd = 0;
    }

    return RT_EOK;
}

/**
  * @brief transmit data
  */
rt_err_t rt_at32_emac_tx(rt_device_t dev, struct pbuf *p)
{
    rt_err_t ret = -RT_ERROR;
    struct pbuf *q;
    rt_uint32_t length = 0;
    rt_uint32_t buffer_offset = 0, payload_offset = 0, copy_count = 0;
    emac_dma_desc_type *dma_tx_desc;
    rt_uint8_t *buffer;

    dma_tx_desc = dma_tx_desc_to_set;
    buffer =  (uint8_t *)(dma_tx_desc_to_set->buf1addr);

    /* copy data to buffer */
    for(q = p; q != NULL; q = q->next)
    {
        if((dma_tx_desc->status & EMAC_DMATXDESC_OWN) != RESET)
        {
            ret = RT_EOK;
            goto _error;
        }

        copy_count = q->len;
        payload_offset = 0;

        while((copy_count + buffer_offset) > EMAC_MAX_PACKET_LENGTH)
        {
            rt_memcpy(buffer + buffer_offset, (uint8_t *)q->payload + payload_offset, (EMAC_MAX_PACKET_LENGTH - buffer_offset));
            dma_tx_desc = (emac_dma_desc_type*)dma_tx_desc->buf2nextdescaddr;

            if((dma_tx_desc->status & EMAC_DMATXDESC_OWN) != RESET)
            {
                ret = RT_EOK;
                goto _error;
            }

            buffer = (uint8_t *)dma_tx_desc->buf1addr;

            copy_count = copy_count - (EMAC_MAX_PACKET_LENGTH - buffer_offset);
            payload_offset = payload_offset + (EMAC_MAX_PACKET_LENGTH - buffer_offset);
            length = length + (EMAC_MAX_PACKET_LENGTH - buffer_offset);
            buffer_offset = 0;
        }

        rt_memcpy(buffer + buffer_offset, (uint8_t *)q->payload + payload_offset, copy_count);
        buffer_offset = buffer_offset + copy_count;
        length = length + copy_count;
    }
    emac_txpkt_chainmode(length);

    ret = RT_EOK;

_error:
     /* when tx buffer unavailable flag is set: clear it and resume transmission */
    if(emac_dma_flag_get(EMAC_DMA_TBU_FLAG))
    {
        /* clear tbus ethernet dma flag */
        emac_dma_flag_clear(EMAC_DMA_TBU_FLAG);
        /* resume dma transmission*/
        EMAC_DMA->tpd_bit.tpd = 0;
    }

    return ret;
}

/**
  * @brief emac rxpkt chainmode
  */
rt_err_t emac_rxpkt_chainmode(void)
{
    /* check if the descriptor is owned by the ethernet dma (when set) or cpu (when reset) */
    if((dma_rx_desc_to_get->status & EMAC_DMARXDESC_OWN) != (u32)RESET)
    {
        /* return error: own bit set */
        return -RT_ERROR;
    }
    if((dma_rx_desc_to_get->status & EMAC_DMARXDESC_LS) != (u32)RESET)
    {
        rx_frame.g_seg_count ++;
        if(rx_frame.g_seg_count == 1)
        {
            rx_frame.rx_fs_desc = dma_rx_desc_to_get;
        }
        rx_frame.rx_ls_desc = dma_rx_desc_to_get;
        rx_frame.length = ((dma_rx_desc_to_get->status & EMAC_DMARXDESC_FL) >> DMARXDESC_FRAMELENGTH_SHIFT) - 4;
        rx_frame.buffer = rx_frame.rx_fs_desc->buf1addr;

        /* Selects the next DMA Rx descriptor list for next buffer to read */
        dma_rx_desc_to_get = (emac_dma_desc_type*) (dma_rx_desc_to_get->buf2nextdescaddr);

        return RT_EOK;
    }
    else if((dma_rx_desc_to_get->status & EMAC_DMARXDESC_FS) != (u32)RESET)
    {
        rx_frame.g_seg_count = 1;
        rx_frame.rx_fs_desc = dma_rx_desc_to_get;
        rx_frame.rx_ls_desc = NULL;
        dma_rx_desc_to_get = (emac_dma_desc_type*) (dma_rx_desc_to_get->buf2nextdescaddr);
    }
    else
    {
        rx_frame.g_seg_count ++;
        dma_rx_desc_to_get = (emac_dma_desc_type*) (dma_rx_desc_to_get->buf2nextdescaddr);
    }

    return -RT_ERROR;
}

/**
  * @brief receive data
  */
struct pbuf *rt_at32_emac_rx(rt_device_t dev)
{
    struct pbuf *p = NULL;
    struct pbuf *q = NULL;
    rt_uint16_t len = 0;
    rt_uint8_t *buffer;

    emac_dma_desc_type *dma_rx_desc;
    rt_uint32_t buffer_offset, payload_offset = 0, copy_count = 0;
    rt_uint32_t index = 0;

    if(emac_rxpkt_chainmode() != RT_EOK)
    {
        return NULL;
    }

    /* obtain the size of the packet and put it into the "len"
       variable. */
    len = rx_frame.length;
    buffer = (uint8_t *)rx_frame.buffer;

    /* we allocate a pbuf chain of pbufs from the pool. */
    if(len > 0)
    {
        p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
    }

    if(p != NULL)
    {
        dma_rx_desc = rx_frame.rx_fs_desc;
        buffer_offset = 0;

        for (q = p; q != NULL; q = q->next)
        {
            copy_count = q->len;
            payload_offset = 0;

            while( (copy_count + buffer_offset) > EMAC_MAX_PACKET_LENGTH )
            {
                /* copy data to pbuf */
                rt_memcpy((uint8_t*)q->payload + payload_offset, buffer + buffer_offset, (EMAC_MAX_PACKET_LENGTH - buffer_offset));

                /* point to next descriptor */
                dma_rx_desc = (emac_dma_desc_type *)(dma_rx_desc->buf2nextdescaddr);
                buffer = (uint8_t *)(dma_rx_desc->buf1addr);

                copy_count = copy_count - (EMAC_MAX_PACKET_LENGTH - buffer_offset);
                payload_offset = payload_offset + (EMAC_MAX_PACKET_LENGTH - buffer_offset);
                buffer_offset = 0;
            }
            rt_memcpy((uint8_t*)q->payload + payload_offset, (uint8_t*)buffer + buffer_offset, copy_count);
            buffer_offset = buffer_offset + copy_count;
        }
    }

    dma_rx_desc = rx_frame.rx_fs_desc;
    for(index = 0; index < rx_frame.g_seg_count; index ++)
    {
        dma_rx_desc->status |= EMAC_DMARXDESC_OWN;
        dma_rx_desc = (emac_dma_desc_type*) (dma_rx_desc->buf2nextdescaddr);
    }

    rx_frame.g_seg_count = 0;

    /* when rx buffer unavailable flag is set: clear it and resume reception */
    if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG))
    {
        /* clear rbus ethernet dma flag */
        emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
        /* resume dma reception */
        EMAC_DMA->rpd_bit.rpd = FALSE;
    }

    return p;
}

void EMAC_IRQHandler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    /* packet receiption */
    if (emac_dma_flag_get(EMAC_DMA_RI_FLAG) == SET)
    {
        /* a frame has been received */
        eth_device_ready(&(at32_emac_device.parent));
        emac_dma_flag_clear(EMAC_DMA_RI_FLAG);
    }

    /* packet transmission */
    if (emac_dma_flag_get(EMAC_DMA_TI_FLAG) == SET)
    {
        emac_dma_flag_clear(EMAC_DMA_TI_FLAG);
    }

    /* clear normal interrupt */
    emac_dma_flag_clear(EMAC_DMA_NIS_FLAG);

    /* clear dma error */
    if(emac_dma_flag_get(EMAC_DMA_AIS_FLAG) != RESET)
    {
        if(emac_dma_flag_get(EMAC_DMA_RBU_FLAG) != RESET)
        {
            emac_dma_flag_clear(EMAC_DMA_RBU_FLAG);
        }
        if(emac_dma_flag_get(EMAC_DMA_OVF_FLAG) != RESET)
        {
            emac_dma_flag_clear(EMAC_DMA_OVF_FLAG);
        }

        emac_dma_flag_clear(EMAC_DMA_AIS_FLAG);
    }

    /* leave interrupt */
    rt_interrupt_leave();
}

enum {
    PHY_LINK        = (1 << 0),
    PHY_10M         = (1 << 1),
    PHY_FULLDUPLEX  = (1 << 2),
};

static void phy_linkchange()
{
    static rt_uint8_t phy_speed = 0;
    rt_uint8_t phy_speed_new = 0;
    rt_uint16_t status;

    emac_phy_register_read(phy_addr, PHY_BASIC_STATUS_REG, (uint16_t *)&status);
    LOG_D("phy basic status reg is 0x%X", status);

    if (status & (PHY_AUTONEGO_COMPLETE_MASK | PHY_LINKED_STATUS_MASK))
    {
        rt_uint16_t SR = 0;

        phy_speed_new |= PHY_LINK;

        emac_phy_register_read(phy_addr, PHY_SPECIFIED_CS_REG, (uint16_t *)&SR);
        LOG_D("phy control status reg is 0x%X", SR);

        if (SR & (PHY_SPEED_MODE))
        {
#if defined (PHY_USING_DP83848)
            phy_speed_new |= PHY_10M;
#endif
        }

        if (SR & (PHY_DUPLEX_MODE))
        {
            phy_speed_new |= PHY_FULLDUPLEX;
        }
    }

    if (phy_speed != phy_speed_new)
    {
        phy_speed = phy_speed_new;
        if (phy_speed & PHY_LINK)
        {
            LOG_D("link up");
            if (phy_speed & PHY_10M)
            {
                LOG_D("10Mbps");
                at32_emac_device.emac_speed = EMAC_SPEED_10MBPS;
            }
            else
            {
                at32_emac_device.emac_speed = EMAC_SPEED_100MBPS;
                LOG_D("100Mbps");
            }

            if (phy_speed & PHY_FULLDUPLEX)
            {
                LOG_D("full-duplex");
                at32_emac_device.emac_mode = EMAC_FULL_DUPLEX;
            }
            else
            {
                LOG_D("half-duplex");
                at32_emac_device.emac_mode = EMAC_HALF_DUPLEX;
            }

            /* send link up. */
            eth_device_linkchange(&at32_emac_device.parent, RT_TRUE);
        }
        else
        {
            LOG_I("link down");
            eth_device_linkchange(&at32_emac_device.parent, RT_FALSE);
        }
    }
}

#ifdef PHY_USING_INTERRUPT_MODE
static void emac_phy_isr(void *args)
{
    rt_uint32_t status = 0;

    emac_phy_register_read(phy_addr, PHY_INTERRUPT_FLAG_REG, (uint16_t *)&status);
    LOG_D("phy interrupt status reg is 0x%X", status);

    phy_linkchange();
}
#endif /* PHY_USING_INTERRUPT_MODE */

static void phy_monitor_thread_entry(void *parameter)
{
    uint8_t detected_count = 0;

    while(phy_addr == 0xFF)
    {
        /* phy search */
        rt_uint32_t i, temp;
        for (i = 0; i <= 0x1F; i++)
        {
            emac_phy_register_read(i, PHY_BASIC_STATUS_REG, (uint16_t *)&temp);

            if (temp != 0xFFFF && temp != 0x00)
            {
                phy_addr = i;
                break;
            }
        }

        detected_count++;
        rt_thread_mdelay(1000);

        if (detected_count > 10)
        {
            LOG_E("No PHY device was detected, please check hardware!");
        }
    }

    LOG_D("Found a phy, address:0x%02X", phy_addr);

    /* reset phy */
    LOG_D("RESET PHY!");
    emac_phy_register_write(phy_addr, PHY_BASIC_CONTROL_REG, PHY_RESET_MASK);
    rt_thread_mdelay(2000);
    emac_phy_register_write(phy_addr, PHY_BASIC_CONTROL_REG, PHY_AUTO_NEGOTIATION_MASK);

    phy_linkchange();
#ifdef PHY_USING_INTERRUPT_MODE
    /* configuration intterrupt pin */
    rt_pin_mode(PHY_INT_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(PHY_INT_PIN, PIN_IRQ_MODE_FALLING, emac_phy_isr, (void *)"callbackargs");
    rt_pin_irq_enable(PHY_INT_PIN, PIN_IRQ_ENABLE);

    /* enable phy interrupt */
    emac_phy_register_write(phy_addr, PHY_INTERRUPT_MASK_REG, PHY_INT_MASK);
#if defined(PHY_INTERRUPT_CTRL_REG)
    emac_phy_register_write(phy_addr, PHY_INTERRUPT_CTRL_REG, PHY_INTERRUPT_EN);
#endif
#else /* PHY_USING_INTERRUPT_MODE */
    at32_emac_device.poll_link_timer = rt_timer_create("phylnk", (void (*)(void*))phy_linkchange,
                                        NULL, RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC);
    if (!at32_emac_device.poll_link_timer || rt_timer_start(at32_emac_device.poll_link_timer) != RT_EOK)
    {
        LOG_E("Start link change detection timer failed");
    }
#endif /* PHY_USING_INTERRUPT_MODE */
}

/* Register the EMAC device */
static int rt_hw_at32_emac_init(void)
{
    rt_err_t state = RT_EOK;

    /* Prepare receive and send buffers */
    rx_buff = (rt_uint8_t *)rt_calloc(EMAC_NUM_RX_BUF, EMAC_MAX_PACKET_LENGTH);
    if (rx_buff == RT_NULL)
    {
        LOG_E("No memory");
        state = -RT_ENOMEM;
        goto __exit;
    }

    tx_buff = (rt_uint8_t *)rt_calloc(EMAC_NUM_TX_BUF, EMAC_MAX_PACKET_LENGTH);
    if (tx_buff == RT_NULL)
    {
        LOG_E("No memory");
        state = -RT_ENOMEM;
        goto __exit;
    }

    dma_rx_dscr_tab = (emac_dma_desc_type *)rt_calloc(EMAC_NUM_RX_BUF, sizeof(emac_dma_desc_type));
    if (dma_rx_dscr_tab == RT_NULL)
    {
        LOG_E("No memory");
        state = -RT_ENOMEM;
        goto __exit;
    }

    dma_tx_dscr_tab = (emac_dma_desc_type *)rt_calloc(EMAC_NUM_TX_BUF, sizeof(emac_dma_desc_type));
    if (dma_tx_dscr_tab == RT_NULL)
    {
        LOG_E("No memory");
        state = -RT_ENOMEM;
        goto __exit;
    }

    /* phy clock */
    phy_clock_config();

    /* enable periph clock */
    crm_periph_clock_enable(CRM_EMAC_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_EMACTX_PERIPH_CLOCK, TRUE);
    crm_periph_clock_enable(CRM_EMACRX_PERIPH_CLOCK, TRUE);

    /* interface mode */
#if defined (SOC_SERIES_AT32F407)
    gpio_pin_remap_config(MII_RMII_SEL_GMUX, TRUE);
#endif
#if defined (SOC_SERIES_AT32F437) || defined (SOC_SERIES_AT32F457)
    scfg_emac_interface_set(SCFG_EMAC_SELECT_RMII);
#endif

    /* emac gpio init */
    at32_msp_emac_init(NULL);

    at32_emac_device.emac_speed = EMAC_SPEED_100MBPS;
    at32_emac_device.emac_mode  = EMAC_FULL_DUPLEX;

    at32_emac_device.dev_addr[0] = 0x00;
    at32_emac_device.dev_addr[1] = 0x66;
    at32_emac_device.dev_addr[2] = 0x88;
    /* generate mac addr from unique id (only for test). */
    at32_emac_device.dev_addr[3] = *(rt_uint8_t *)(0x1FFFF7E8 + 4);
    at32_emac_device.dev_addr[4] = *(rt_uint8_t *)(0x1FFFF7E8 + 2);
    at32_emac_device.dev_addr[5] = *(rt_uint8_t *)(0x1FFFF7E8 + 0);

    at32_emac_device.parent.parent.init = rt_at32_emac_init;
    at32_emac_device.parent.parent.open = rt_at32_emac_open;
    at32_emac_device.parent.parent.close = rt_at32_emac_close;
    at32_emac_device.parent.parent.read = rt_at32_emac_read;
    at32_emac_device.parent.parent.write = rt_at32_emac_write;
    at32_emac_device.parent.parent.control = rt_at32_emac_control;
    at32_emac_device.parent.parent.user_data = RT_NULL;

    at32_emac_device.parent.eth_rx = rt_at32_emac_rx;
    at32_emac_device.parent.eth_tx = rt_at32_emac_tx;

    rx_frame.g_seg_count = 0;

    /* reset phy */
    phy_reset();

    /* start phy monitor */
    rt_thread_t tid;
    tid = rt_thread_create("phy",
                           phy_monitor_thread_entry,
                           RT_NULL,
                           1024,
                           RT_THREAD_PRIORITY_MAX - 2,
                           2);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        state = -RT_ERROR;
    }

    /* register eth device */
    state = eth_device_init(&(at32_emac_device.parent), "e0");
    if (RT_EOK == state)
    {
        LOG_D("emac device init success");
    }
    else
    {
        LOG_E("emac device init faild: %d", state);
        state = -RT_ERROR;
        goto __exit;
    }
__exit:
    if (state != RT_EOK)
    {
        if (rx_buff)
        {
            rt_free(rx_buff);
        }

        if (tx_buff)
        {
            rt_free(tx_buff);
        }

        if (dma_rx_dscr_tab)
        {
            rt_free(dma_rx_dscr_tab);
        }

        if (dma_tx_dscr_tab)
        {
            rt_free(dma_tx_dscr_tab);
        }
    }

    return state;
}

INIT_DEVICE_EXPORT(rt_hw_at32_emac_init);