summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/drivers/eth/fxmac/fxmac_options.c
blob: c4e4b0d570d55ff6515eab2f6f6b4c5bcacc1be7 (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
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fxmac_options.c
 * Date: 2022-04-06 14:46:52
 * LastEditTime: 2022-04-06 14:46:58
 * Description:  This file is for
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 */

#include "fxmac_hw.h"
#include "fxmac.h"
#include "fassert.h"


/**
 * @name: FXmacSetMacAddress
 *
 * @msg: Set the MAC address for this driver/device.  The address is a 48-bit value.
 * The device must be stopped before calling this function.
 *
 * @param {FXmac *}: instance_p is a pointer to the instance to be worked on.
 * @param address_ptr is a pointer to a 6-byte MAC address.
 * @param index plus 1 is a index to which MAC (1-4) address.
 *
 * @return
 * - FT_SUCCESS if the MAC address was set successfully
 * - FXMAC_ERR_MAC_IS_PROCESSING if the device has not yet been stopped
 *
 */
FError FXmacSetMacAddress(FXmac *instance_p, u8 *address_ptr, u8 index)
{
    u32 mac_addr;
    u8 *aptr = (u8 *)(void *)address_ptr;
    u8 index_loc = index;
    FError status;
    FASSERT(instance_p != NULL);
    FASSERT(aptr != NULL);
    FASSERT(instance_p->is_ready == (u32)FT_COMPONENT_IS_READY);
    FASSERT((index_loc < (u8)FXMAC_MAX_MAC_ADDR));

    /* Be sure device has been stopped */
    if (instance_p->is_started == (u32)FT_COMPONENT_IS_STARTED)
    {
        status = (FError)(FXMAC_ERR_MAC_IS_PROCESSING);
    }
    else
    {
        /* Set the MAC bits [31:0] in BOT */
        mac_addr = *(aptr);
        mac_addr |= ((u32)(*(aptr + 1)) << 8U);
        mac_addr |= ((u32)(*(aptr + 2)) << 16U);
        mac_addr |= ((u32)(*(aptr + 3)) << 24U);
        FXMAC_WRITEREG32(instance_p->config.base_address,
                         ((u32)FXMAC_GEM_SA1B + ((u32)index_loc * (u32)8)), mac_addr);

        /* There are reserved bits in TOP so don't affect them */
        mac_addr = FXMAC_READREG32(instance_p->config.base_address,
                                   ((u32)FXMAC_GEM_SA1T + ((u32)index_loc * (u32)8)));

        mac_addr &= (u32)(~FXMAC_GEM_SAB_MASK);

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

        FXMAC_WRITEREG32(instance_p->config.base_address,
                         ((u32)FXMAC_GEM_SA1T + ((u32)index_loc * (u32)8)), mac_addr);

        status = (FError)(FT_SUCCESS);
    }
    return status;
}

/**
 * @name: FXmacGetMacAddress
 * @msg:  Set the MAC address according to index
 * @param {FXmac} *mac is a pointer to the instance to be worked on.
 * @param {void} *address_ptr is an output parameter, and is a pointer to a buffer into
 *        which the current MAC address will be copied.
 * @param {u8} index is a index to which MAC (0-3) address.
 */
void FXmacGetMacAddress(FXmac *instance_p, u8 *address_ptr, u8 index)
{
    u32 reg_value;
    u8 *ptr = (u8 *)address_ptr;
    FASSERT(instance_p != NULL);
    FASSERT(ptr != NULL);
    FASSERT(instance_p->is_ready == FT_COMPONENT_IS_READY);
    FASSERT((index < FXMAC_MAX_MAC_ADDR));

    reg_value = FXMAC_READREG32(instance_p->config.base_address, FXMAC_GEM_SA1B + ((u32)index * (u32)8));
    *ptr = (u8)reg_value;
    *(ptr + 1) = (u8)(reg_value >> 8U);
    *(ptr + 2) = (u8)(reg_value >> 16U);
    *(ptr + 3) = (u8)(reg_value >> 24U);

    reg_value = FXMAC_READREG32(instance_p->config.base_address, FXMAC_GEM_SA1T + ((u32)index * (u32)8));
    *(ptr + 4) = (u8)(reg_value);
    *(ptr + 5) = (u8)(reg_value >> 8U);
}


/**
 * 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.
 *
 * @param instance_p is a pointer to the instance to be worked on.
 * @param id_check is type ID to be configured.
 * @param index plus 1 is a index to which Type ID (1-4).
 *
 * @return
 * - FT_SUCCESS if the MAC address was set successfully
 * - FXMAC_ERR_MAC_IS_PROCESSING if the device has not yet been stopped
 *
 */
FError FXmacSetTypeIdCheck(FXmac *instance_p, u32 id_check, u8 index)
{
    u8 index_loc = index;
    FError status;
    FASSERT(instance_p != NULL);
    FASSERT(instance_p->is_ready == FT_COMPONENT_IS_READY);
    FASSERT((index_loc < (u8)FXMAC_MAX_TYPE_ID));

    /* Be sure device has been stopped */
    if (instance_p->is_started == (u32)FT_COMPONENT_IS_STARTED)
    {
        status = (FError)(FXMAC_ERR_MAC_IS_PROCESSING);
    }
    else
    {

        /* Set the ID bits in MATCHx register */
        FXMAC_WRITEREG32(instance_p->config.base_address,
                         ((u32)FXMAC_MATCH1_OFFSET + ((u32)index_loc * (u32)4)), id_check);

        status = (FError)(FT_SUCCESS);
    }
    return status;
}


/**
 * Set options for the driver/device. The driver should be stopped with
 * FXmacStop() before changing options.
 *
 * @param instance_p is a pointer to the instance to be worked on.
 * @param options are the options to set. Multiple options can be set by OR'ing
 *        FXMAC_*_OPTIONS constants together. options not specified are not
 *        affected.
 * @param queue_num is the Buffer Queue Index ,Used for jumbo frames only
 *
 * @return
 * - FT_SUCCESS if the options were set successfully
 * - FXMAC_ERR_MAC_IS_PROCESSING if the device has not yet been stopped
 *
 * @note
 * See fxmac.h for a description of the available options.
 *
 */
FError FXmacSetOptions(FXmac *instance_p, u32 options, u32 queue_num)
{
    u32 reg;            /* Generic register contents */
    u32 reg_netcfg;     /* Reflects original contents of NET_CONFIG */
    u32 reg_new_netcfg; /* Reflects new contents of NET_CONFIG */
    FError status;
    FXmacConfig *config_p;
    FASSERT(instance_p != NULL);
    FASSERT(instance_p->is_ready == (u32)FT_COMPONENT_IS_READY);
    config_p = &instance_p->config;
    /* Be sure device has been stopped */
    if (instance_p->is_started == (u32)FT_COMPONENT_IS_STARTED)
    {
        status = (FError)(FXMAC_ERR_MAC_IS_PROCESSING);
    }
    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 = FXMAC_READREG32(config_p->base_address,
                                     FXMAC_NWCFG_OFFSET);
        reg_new_netcfg = reg_netcfg;

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

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

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

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

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

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

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

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

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

        /* Enable jumbo frames */
        if ((options & FXMAC_JUMBO_ENABLE_OPTION) != 0x00000000U)
        {
            instance_p->max_mtu_size = FXMAC_MTU_JUMBO;
            instance_p->max_frame_size = FXMAC_MTU_JUMBO +
                                         FXMAC_HDR_SIZE + FXMAC_TRL_SIZE;

            reg_new_netcfg |= FXMAC_NWCFG_JUMBO_MASK;
            FXMAC_WRITEREG32(config_p->base_address,
                             FXMAC_JUMBOMAXLEN_OFFSET, FXMAC_MTU_JUMBO);

            if (queue_num == 0)
            {
                u32 rx_buf_size = 0;
                reg = FXMAC_READREG32(config_p->base_address,
                                      FXMAC_DMACR_OFFSET);
                reg &= ~FXMAC_DMACR_RXBUF_MASK;

                rx_buf_size = ((u32)instance_p->max_mtu_size / (u32)FXMAC_RX_BUF_UNIT);
                rx_buf_size += (((u32)instance_p->max_mtu_size % (u32)FXMAC_RX_BUF_UNIT) != (u32)0) ? 1U : 0U;

                reg |= ((rx_buf_size << (u32)(FXMAC_DMACR_RXBUF_SHIFT)) &
                        (u32)(FXMAC_DMACR_RXBUF_MASK));
                FXMAC_WRITEREG32(config_p->base_address,
                                 FXMAC_DMACR_OFFSET, reg);
            }
            else if (queue_num < instance_p->config.max_queue_num)
            {
                u32 rx_buf_size = 0;
                rx_buf_size = ((u32)instance_p->max_mtu_size / (u32)FXMAC_RX_BUF_UNIT);
                rx_buf_size += (((u32)instance_p->max_mtu_size % (u32)FXMAC_RX_BUF_UNIT) != (u32)0) ? 1U : 0U;

                FXMAC_WRITEREG32(config_p->base_address, FXMAC_RXBUFQX_SIZE_OFFSET(queue_num), rx_buf_size & FXMAC_RXBUFQX_SIZE_MASK);
            }
        }

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

        if ((options & FXMAC_LOOPBACK_NO_MII_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(config_p->base_address, FXMAC_NWCTRL_OFFSET);
            reg |= FXMAC_NWCFG_LOOPBACK_LOCAL_MASK;
            FXMAC_WRITEREG32(config_p->base_address, FXMAC_NWCTRL_OFFSET, reg);
        }

        if ((options & FXMAC_LOOPBACK_USXGMII_OPTION) != 0x00000000U)
        {
            FXMAC_WRITEREG32(config_p->base_address, FXMAC_TEST_CONTROL_OFFSET, 2);
        }

        /* Officially change the NET_CONFIG registers if it needs to be
         * modified.
         */
        if (reg_netcfg != reg_new_netcfg)
        {
            FXMAC_WRITEREG32(config_p->base_address,
                             FXMAC_NWCFG_OFFSET, reg_new_netcfg);
        }

        /* Enable TX checksum offload */
        if ((options & FXMAC_TX_CHKSUM_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(config_p->base_address,
                                  FXMAC_DMACR_OFFSET);
            reg |= FXMAC_DMACR_TCPCKSUM_MASK;
            FXMAC_WRITEREG32(config_p->base_address,
                             FXMAC_DMACR_OFFSET, reg);
        }

        /* Enable transmitter */
        if ((options & FXMAC_TRANSMITTER_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(config_p->base_address,
                                  FXMAC_NWCTRL_OFFSET);
            reg |= FXMAC_NWCTRL_TXEN_MASK;
            FXMAC_WRITEREG32(config_p->base_address,
                             FXMAC_NWCTRL_OFFSET, reg);
        }

        /* Enable receiver */
        if ((options & FXMAC_RECEIVER_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(config_p->base_address,
                                  FXMAC_NWCTRL_OFFSET);
            reg |= FXMAC_NWCTRL_RXEN_MASK;
            FXMAC_WRITEREG32(config_p->base_address,
                             FXMAC_NWCTRL_OFFSET, 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 = (FError)(FT_SUCCESS);
    }
    return status;
}


/**
 * Clear options for the driver/device
 *
 * @param instance_p is a pointer to the instance to be worked on.
 * @param options are the options to clear. Multiple options can be cleared by
 *        OR'ing FXMAC_*_options constants together. options not specified
 *        are not affected.
 * @param queue_num is the Buffer Queue Index ,Used for jumbo frames only
 * @return
 * - FT_SUCCESS if the options were set successfully
 * - FXMAC_ERR_MAC_IS_PROCESSING if the device has not yet been stopped
 *
 * @note
 * See fxmac.h for a description of the available options.
 */
FError FXmacClearOptions(FXmac *instance_p, u32 options, u32 queue_num)
{
    u32 reg;             /* Generic */
    u32 reg_net_cfg;     /* Reflects original contents of NET_CONFIG */
    u32 reg_new_net_cfg; /* Reflects new contents of NET_CONFIG */
    FError status;
    FXmacConfig *config_p;
    FASSERT(instance_p != NULL);
    FASSERT(instance_p->is_ready == (u32)FT_COMPONENT_IS_READY);
    config_p = &instance_p->config;
    /* Be sure device has been stopped */
    if (instance_p->is_started == (u32)FT_COMPONENT_IS_STARTED)
    {
        status = (FError)(FXMAC_ERR_MAC_IS_PROCESSING);
    }
    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 = FXMAC_READREG32(instance_p->config.base_address,
                                      FXMAC_NWCFG_OFFSET);
        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) != 0x00000000U)
        {
            reg_new_net_cfg &= (u32)(~FXMAC_NWCFG_1536RXEN_MASK);
        }

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

        /* Turn off FCS stripping on receive packets */
        if ((options & FXMAC_FCS_STRIP_OPTION) != 0x00000000U)
        {
            reg_new_net_cfg &= (u32)(~FXMAC_NWCFG_FCSREM_MASK);
        }

        /* Turn off length/type field checking on receive packets */
        if ((options & FXMAC_LENTYPE_ERR_OPTION) != 0x00000000U)
        {
            reg_new_net_cfg &= (u32)(~FXMAC_NWCFG_LENERRDSCRD_MASK);
        }

        /* Turn off flow control */
        if ((options & FXMAC_FLOW_CONTROL_OPTION) != 0x00000000U)
        {
            reg_new_net_cfg &= (u32)(~FXMAC_NWCFG_PAUSEEN_MASK);
        }

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

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

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

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

        /* Disable jumbo frames */
        if (((options & FXMAC_JUMBO_ENABLE_OPTION) != 0x00000000U)) /* 恢复之前buffer 容量 */
        {

            instance_p->max_mtu_size = FXMAC_MTU;
            instance_p->max_frame_size = FXMAC_MTU + FXMAC_HDR_SIZE + FXMAC_TRL_SIZE;

            reg_new_net_cfg &= (u32)(~FXMAC_NWCFG_JUMBO_MASK);
            reg = FXMAC_READREG32(instance_p->config.base_address,
                                  FXMAC_DMACR_OFFSET);
            reg &= ~FXMAC_DMACR_RXBUF_MASK;

            if (queue_num == 0)
            {
                u32 rx_buf_size = 0;

                reg = FXMAC_READREG32(instance_p->config.base_address, FXMAC_DMACR_OFFSET);
                reg &= ~FXMAC_DMACR_RXBUF_MASK;

                rx_buf_size = ((u32)instance_p->max_mtu_size / (u32)FXMAC_RX_BUF_UNIT);
                rx_buf_size += ((u32)instance_p->max_mtu_size % ((u32)FXMAC_RX_BUF_UNIT) != (u32)0) ? 1U : 0U;

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

                FXMAC_WRITEREG32(instance_p->config.base_address, FXMAC_DMACR_OFFSET, reg);
            }
            else if (queue_num < instance_p->config.max_queue_num)
            {
                u32 rx_buf_size = 0;
                rx_buf_size = ((u32)instance_p->max_mtu_size / (u32)FXMAC_RX_BUF_UNIT);
                rx_buf_size += (((u32)instance_p->max_mtu_size % (u32)FXMAC_RX_BUF_UNIT) != (u32)0) ? 1U : 0U;

                FXMAC_WRITEREG32(config_p->base_address, FXMAC_RXBUFQX_SIZE_OFFSET(queue_num), rx_buf_size & FXMAC_RXBUFQX_SIZE_MASK);
            }
        }

        if (((options & FXMAC_SGMII_ENABLE_OPTION) != 0x00000000U))
        {
            reg_new_net_cfg &= (u32)(~(FXMAC_NWCFG_SGMIIEN_MASK |
                                       FXMAC_NWCFG_PCSSEL_MASK));
        }

        if ((options & FXMAC_LOOPBACK_NO_MII_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(config_p->base_address, FXMAC_NWCTRL_OFFSET);
            reg &= (u32)(~FXMAC_NWCFG_LOOPBACK_LOCAL_MASK);
            FXMAC_WRITEREG32(config_p->base_address, FXMAC_NWCTRL_OFFSET, reg);
        }

        if ((options & FXMAC_LOOPBACK_USXGMII_OPTION) != 0x00000000U)
        {
            FXMAC_WRITEREG32(config_p->base_address, FXMAC_TEST_CONTROL_OFFSET, (FXMAC_READREG32(config_p->base_address, FXMAC_TEST_CONTROL_OFFSET) & ~2));
        }

        /* Officially change the NET_CONFIG registers if it needs to be
         * modified.
         */
        if (reg_net_cfg != reg_new_net_cfg)
        {
            FXMAC_WRITEREG32(instance_p->config.base_address,
                             FXMAC_NWCFG_OFFSET, reg_new_net_cfg);
        }

        /* Disable TX checksum offload */
        if ((options & FXMAC_TX_CHKSUM_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(instance_p->config.base_address,
                                  FXMAC_DMACR_OFFSET);
            reg &= (u32)(~FXMAC_DMACR_TCPCKSUM_MASK);
            FXMAC_WRITEREG32(instance_p->config.base_address,
                             FXMAC_DMACR_OFFSET, reg);
        }

        /* Disable transmitter */
        if ((options & FXMAC_TRANSMITTER_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(instance_p->config.base_address,
                                  FXMAC_NWCTRL_OFFSET);
            reg &= (u32)(~FXMAC_NWCTRL_TXEN_MASK);
            FXMAC_WRITEREG32(instance_p->config.base_address,
                             FXMAC_NWCTRL_OFFSET, reg);
        }

        /* Disable receiver */
        if ((options & FXMAC_RECEIVER_ENABLE_OPTION) != 0x00000000U)
        {
            reg = FXMAC_READREG32(instance_p->config.base_address,
                                  FXMAC_NWCTRL_OFFSET);
            reg &= (u32)(~FXMAC_NWCTRL_RXEN_MASK);
            FXMAC_WRITEREG32(instance_p->config.base_address,
                             FXMAC_NWCTRL_OFFSET, 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 = (FError)(FT_SUCCESS);
    }
    return status;
}


/**
 * Clear the Hash registers for the mac address pointed by address_ptr.
 *
 * @param instance_p is a pointer to the instance to be worked on.
 *
 */
void FXmacClearHash(FXmac *instance_p)
{
    FASSERT(instance_p != NULL);
    FASSERT(instance_p->is_ready == (u32)FT_COMPONENT_IS_READY);

    FXMAC_WRITEREG32(instance_p->config.base_address,
                     FXMAC_HASHL_OFFSET, 0x0U);

    /* write bits [63:32] in TOP */
    FXMAC_WRITEREG32(instance_p->config.base_address,
                     FXMAC_HASHH_OFFSET, 0x0U);
}


/**
 * Write data to the specified PHY register. The Ethernet driver does not
 * require the device to be stopped before writing to the PHY.  Although it is
 * probably a good idea to stop the device, it is the responsibility of the
 * application to deem this necessary. The MAC provides the driver with the
 * ability to talk to a PHY that adheres to the Media Independent Interface
 * (MII) as defined in the IEEE 802.3 standard.
 *
 * Prior to PHY access with this function, the user should have setup the MDIO
 * clock with FXmacSetMdioDivisor().
 *
 * @param instance_p is a pointer to the FXmac instance to be worked on.
 * @param phy_address is the address of the PHY to be written (supports multiple
 *        PHYs)
 * @param register_num is the register number, 0-31, of the specific PHY register
 *        to write
 * @param phy_data is the 16-bit value that will be written to the register
 *
 * @return
 *
 * - FT_SUCCESS if the PHY was written to successfully. Since there is no error
 *   status from the MAC on a write, the user should read the PHY to verify the
 *   write was successful.
 * - FXMAC_ERR_PHY_BUSY if there is another PHY operation in progress
 *
 * @note
 *
 * This function is not thread-safe. The user must provide mutually exclusive
 * access to this function if there are to be multiple threads that can call it.
 *
 * There is the possibility that this function will not return if the hardware
 * is broken (i.e., it never sets the status bit indicating that the write is
 * done). If this is of concern to the user, the user should provide a mechanism
 * suitable to their needs for recovery.
 *
 * For the duration of this function, all host interface reads and writes are
 * blocked to the current FXmac instance.
 *
 ******************************************************************************/
FError FXmacPhyWrite(FXmac *instance_p, u32 phy_address,
                     u32 register_num, u16 phy_data)
{
    u32 mgtcr;
    volatile u32 ipisr;
    u32 ip_write_temp;
    FError status;

    FASSERT(instance_p != NULL);

    /* Make sure no other PHY operation is currently in progress */
    if ((!(FXMAC_READREG32(instance_p->config.base_address,
                           FXMAC_NWSR_OFFSET) &
            FXMAC_NWSR_MDIOIDLE_MASK)) == TRUE)
    {
        status = (FError)(FXMAC_ERR_PHY_BUSY);
    }
    else
    {
        /* Construct mgtcr mask for the operation */
        mgtcr = FXMAC_PHYMNTNC_OP_MASK | FXMAC_PHYMNTNC_OP_W_MASK |
                (phy_address << FXMAC_PHYMNTNC_PHAD_SHFT_MSK) |
                (register_num << FXMAC_PHYMNTNC_PREG_SHFT_MSK) | (u32)phy_data;

        /* Write mgtcr and wait for completion */
        FXMAC_WRITEREG32(instance_p->config.base_address,
                         FXMAC_PHYMNTNC_OFFSET, mgtcr);

        do
        {
            ipisr = FXMAC_READREG32(instance_p->config.base_address,
                                    FXMAC_NWSR_OFFSET);
            ip_write_temp = ipisr;
        }
        while ((ip_write_temp & FXMAC_NWSR_MDIOIDLE_MASK) == 0x00000000U);

        status = (FError)(FT_SUCCESS);
    }
    return status;
}


/**
 * Read the current value of the PHY register indicated by the phy_address and
 * the register_num parameters. The MAC provides the driver with the ability to
 * talk to a PHY that adheres to the Media Independent Interface (MII) as
 * defined in the IEEE 802.3 standard.
 *
 *
 * @param instance_p is a pointer to the FXmac instance to be worked on.
 * @param phy_address is the address of the PHY to be read (supports multiple
 *        PHYs)
 * @param register_num is the register number, 0-31, of the specific PHY register
 *        to read
 * @param phydat_aptr is an output parameter, and points to a 16-bit buffer into
 *        which the current value of the register will be copied.
 *
 * @return
 *
 * - FT_SUCCESS if the PHY was read from successfully
 * - FXMAC_ERR_PHY_BUSY if there is another PHY operation in progress
 *
 * @note
 *
 * This function is not thread-safe. The user must provide mutually exclusive
 * access to this function if there are to be multiple threads that can call it.
 *
 * There is the possibility that this function will not return if the hardware
 * is broken (i.e., it never sets the status bit indicating that the read is
 * done). If this is of concern to the user, the user should provide a mechanism
 * suitable to their needs for recovery.
 *
 * For the duration of this function, all host interface reads and writes are
 * blocked to the current FXmac instance.
 *
 ******************************************************************************/
FError FXmacPhyRead(FXmac *instance_p, u32 phy_address,
                    u32 register_num, u16 *phydat_aptr)
{
    u32 mgtcr;
    volatile u32 ipisr;
    u32 IpReadTemp;
    FError status;

    FASSERT(instance_p != NULL);

    /* Make sure no other PHY operation is currently in progress */
    if ((!(FXMAC_READREG32(instance_p->config.base_address,
                           FXMAC_NWSR_OFFSET) &
            FXMAC_NWSR_MDIOIDLE_MASK)) == TRUE)
    {
        status = (FError)(FXMAC_ERR_PHY_BUSY);
    }
    else
    {
        /* Construct mgtcr mask for the operation */
        mgtcr = FXMAC_PHYMNTNC_OP_MASK | FXMAC_PHYMNTNC_OP_R_MASK |
                (phy_address << FXMAC_PHYMNTNC_PHAD_SHFT_MSK) |
                (register_num << FXMAC_PHYMNTNC_PREG_SHFT_MSK);

        /* Write mgtcr and wait for completion */
        FXMAC_WRITEREG32(instance_p->config.base_address,
                         FXMAC_PHYMNTNC_OFFSET, mgtcr);

        do
        {
            ipisr = FXMAC_READREG32(instance_p->config.base_address,
                                    FXMAC_NWSR_OFFSET);
            IpReadTemp = ipisr;
        }
        while ((IpReadTemp & FXMAC_NWSR_MDIOIDLE_MASK) == 0x00000000U);

        /* Read data */
        *phydat_aptr = (u16)FXMAC_READREG32(instance_p->config.base_address,
                                            FXMAC_PHYMNTNC_OFFSET);
        status = (FError)(FT_SUCCESS);
    }
    return status;
}