summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/drivers/eth/fgmac/fgmac.c
blob: bef00ec931b7888a3f5cfc4aa32fe7ba5158d117 (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
/*
 * 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: fgmac.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 Files *********************************/
#include <string.h>

#include "fio.h"
#include "ferror_code.h"
#include "ftypes.h"
#include "fdebug.h"
#include "fassert.h"
#include "fgmac_hw.h"
#include "fgmac_phy.h"
#include "fgmac.h"

/************************** Constant Definitions *****************************/

/**************************** Type Definitions *******************************/

/***************** Macros (Inline Functions) Definitions *********************/
#define FGMAC_DEBUG_TAG "FGMAC"
#define FGMAC_ERROR(format, ...)   FT_DEBUG_PRINT_E(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FGMAC_WARN(format, ...)    FT_DEBUG_PRINT_W(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FGMAC_INFO(format, ...)    FT_DEBUG_PRINT_I(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)
#define FGMAC_DEBUG(format, ...)   FT_DEBUG_PRINT_D(FGMAC_DEBUG_TAG, format, ##__VA_ARGS__)


/************************** Function Prototypes ******************************/
static FError FGmacReset(FGmac *instance_p);
static FError FGmacDmaConfigure(FGmac *instance_p);
static FError FGmacControllerConfigure(FGmac *instance_p);
/************************** Variable Definitions *****************************/

/*****************************************************************************/

/* 此文件主要为了完成用户对外接口,用户可以使用这些接口直接开始工作 */

/* - 包括用户API的定义和实现
   - 同时包含必要的OPTION方法,方便用户进行配置
   - 如果驱动可以直接进行I/O操作,在此源文件下可以将API 进行实现 */

/* - 包括用户API的定义和实现
   - 同时包含必要的OPTION方法,方便用户进行配置
   - 如果驱动可以直接进行I/O操作,在此源文件下可以将API 进行实现 */

/*
 * @name: FGmacStop
 * @msg: 停止FGMAC控制器寄存器
 * @return {*}
 * @param {FGmac} *instance_p 驱动控制数据
 */
void FGmacStop(FGmac *instance_p)
{
    FASSERT(instance_p);
    uintptr base_addr = instance_p->config.base_addr;
    u32 reg_val;

    /* disable dma tx and rx */
    reg_val = FGMAC_READ_REG32(base_addr, FGMAC_DMA_OP_OFFSET);
    reg_val &= (~FGMAC_DMA_OP_ST);
    reg_val &= (~FGMAC_DMA_OP_SR);
    FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_OP_OFFSET, reg_val);

    /* disable gmac tx and rx */
    reg_val = FGMAC_READ_REG32(base_addr, FGMAC_CONF_OFFSET);
    reg_val &= (~FGMAC_CONF_TX_EN);
    reg_val &= (~FGMAC_CONF_RX_EN);
    FGMAC_WRITE_REG32(base_addr, FGMAC_CONF_OFFSET, reg_val);

}


/**
 * @name: FGmacCfgInitialize
 * @msg: init FGMAC controller
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @param {FGmacConfig} *cofig_p, input configuration parameters
 * @return err code information, FGMAC_SUCCESS indicates success,others indicates failed
 */
FError FGmacCfgInitialize(FGmac *instance_p, const FGmacConfig *input_config_p)
{
    FASSERT(instance_p && input_config_p);
    FError ret = FGMAC_SUCCESS;

    /* indicating device is started */
    if (FT_COMPONENT_IS_READY == instance_p->is_ready)
    {
        FGMAC_WARN("device is already initialized!!!");
    }

    /* de-initialize device instance */
    FGmacDeInitialize(instance_p);
    instance_p->config = *input_config_p;


    /*Phy Awaken*/

    ret = FGmacPhyAwaken(instance_p);
    if (FGMAC_SUCCESS != ret)
    {
        FGMAC_ERROR("phy awaken failed!");
        return ret;
    }


    /* initialize the gmac controller */
    ret = FGmacReset(instance_p); // permmit failed
    if (FGMAC_SUCCESS != ret)
    {
        printf("FGmacReset failed \r\n");
    }


    ret = FGmacControllerConfigure(instance_p);
    if (FGMAC_SUCCESS != ret)
        return ret;

    /* initialize the gmac dma controller */
    ret = FGmacDmaConfigure(instance_p);
    if (FGMAC_SUCCESS == ret)
    {
        instance_p->is_ready = FT_COMPONENT_IS_READY;
    }

    return ret;
}

/**
 * @name: FGmacDeInitialize
 * @msg: deinit FGMAC controller
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @return err code information, FGMAC_SUCCESS indicates success,others indicates failed
 */
FError FGmacDeInitialize(FGmac *instance_p)
{
    FASSERT(instance_p);
    FError ret = FGMAC_SUCCESS;

    instance_p->is_ready = 0;
    memset(instance_p, 0, sizeof(*instance_p));

    return ret;
}

/**
 * @name: FGmacReset
 * @msg: reset FGMAC controller
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @return err code information, FGMAC_SUCCESS indicates success,others indicates failed
 */
static FError FGmacReset(FGmac *instance_p)
{
    FASSERT(instance_p);
    FGmacMacAddr mac_addr;
    uintptr base_addr = instance_p->config.base_addr;
    FError ret = FGMAC_SUCCESS;
    u32 reg_val;


    /* backup mac address before software reset */
    memset(mac_addr, 0, sizeof(mac_addr));
    FGmacGetMacAddr(base_addr, mac_addr);

    /* disable all gmac & dma intr */
    FGmacSetInterruptMask(instance_p, FGMAC_CTRL_INTR, FGMAC_ISR_MASK_ALL_BITS);
    FGmacSetInterruptMask(instance_p, FGMAC_DMA_INTR, FGMAC_DMA_INTR_ENA_ALL_MASK);

    /* stop gmac/dma tx and rx */
    FGmacStop(instance_p);

    /* do software reset per init */
    ret = FGmacSoftwareReset(base_addr, FGMAC_RETRY_TIMES);


    /* disable gmac & dma interrupts */
    FGmacSetInterruptMask(instance_p, FGMAC_CTRL_INTR, FGMAC_ISR_MASK_ALL_BITS);
    FGmacSetInterruptMask(instance_p, FGMAC_DMA_INTR, FGMAC_DMA_INTR_ENA_ALL_MASK);

    /* recover mac address after softwate reset */
    FGmacSetMacAddr(base_addr, mac_addr);

    return ret;// may return error
}

/**
 * @name: FGmacControllerSpeedConfig
 * @msg: fgmac speed configuration
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @param {u32} speed, speed value
 * @return {*}
 */
void FGmacControllerSpeedConfig(FGmac *instance_p, u32 speed)
{
    FASSERT(instance_p);
    uintptr base_addr = instance_p->config.base_addr;
    u32 reg_val = 0;

    /* MAC配置寄存器 */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_CONF_OFFSET);

    /* 设置通信速度FES=1000Mbps */
    if (speed == FGMAC_PHY_SPEED_1000)
    {
        reg_val &= (~FGMAC_CONF_PORTSELECT);
        reg_val &= (~FGMAC_CONF_FES);
    }

    /* 设置通信速度FES=100Mbps */
    if (speed == FGMAC_PHY_SPEED_100)
    {
        reg_val |= FGMAC_CONF_PORTSELECT;
        reg_val |= FGMAC_CONF_FES;
    }

    /* 设置通信速度FES=10Mbps */
    if (speed == FGMAC_PHY_SPEED_10)
    {
        reg_val |= FGMAC_CONF_PORTSELECT;
        reg_val &= (~FGMAC_CONF_FES);
    }

    FGMAC_WRITE_REG32(base_addr, FGMAC_CONF_OFFSET, reg_val);
}

/**
 * @name: FGmacControllerDuplexConfig
 * @msg: fgmac deplex mode configuration
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @param {u32} duplex, duplex mode
 * @return {*}
 */
void FGmacControllerDuplexConfig(FGmac *instance_p, u32 duplex)
{
    FASSERT(instance_p);
    uintptr base_addr = instance_p->config.base_addr;
    u32 reg_val = 0;

    /* MAC配置寄存器 */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_CONF_OFFSET);

    /* 设置双工模式 */
    if (duplex == FGMAC_PHY_MODE_FULLDUPLEX)
        reg_val |= FGMAC_CONF_DUPLEX_MODE;
    else
        reg_val &= ~FGMAC_CONF_DUPLEX_MODE;

    FGMAC_WRITE_REG32(base_addr, FGMAC_CONF_OFFSET, reg_val);

}

/**
 * @name: FGmacControllerConfigure
 * @msg: config FGMAC controller
 * @param {FGmac} *instance_p, instance of FGmac controller
 * @return err code information, FGMAC_SUCCESS indicates success,others indicates failed
 */
static FError FGmacControllerConfigure(FGmac *instance_p)
{
    FASSERT(instance_p);
    FGmacMacAddr mac_addr;
    uintptr base_addr = instance_p->config.base_addr;
    FError ret = FGMAC_SUCCESS;
    u32 reg_val = 0;

    /********gmac ctrl reg init*********/
    /* Set the WD bit according to ETH Watchdog value */
    /* Set the JD: bit according to ETH Jabber value */
    /* Set the IFG bit according to ETH InterFrameGap value */
    /* Set the DCRS bit according to ETH CarrierSense value */
    /* Set the FES bit according to ETH Speed value */
    /* Set the DO bit according to ETH ReceiveOwn value */
    /* Set the LM bit according to ETH LoopbackMode value */
    /* Set the DM bit according to ETH Mode value */
    /* Set the IPCO bit according to ETH ChecksumOffload value */
    /* Set the DR bit according to ETH RetryTransmission value */
    /* Set the ACS bit according to ETH AutomaticPadCRCStrip value */
    /* Set the BL bit according to ETH BackOffLimit value */
    /* Set the DC bit according to ETH DeferralCheck value */

    /* MAC配置寄存器 */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_CONF_OFFSET);

    /* 使能载波侦听DCRS、失能环回模式LM */
    reg_val &= ~(FGMAC_CONF_DCRS | FGMAC_CONF_LOOPBACK_MODE);

    /* 设置帧内间隔IFG */
    reg_val |= FGMAC_CONF_IFG(0);

    /* 1000Mbps default */
    reg_val &= (~FGMAC_CONF_PORTSELECT);
    reg_val &= (~FGMAC_CONF_FES);

    /* 双工模式 */
    if (instance_p->config.duplex_mode)
        reg_val |= FGMAC_CONF_DUPLEX_MODE;
    else
        reg_val &= ~FGMAC_CONF_DUPLEX_MODE;

    /* 使能校验和卸载IPS */
    if (FGMAC_CHECKSUM_BY_HARDWARE == instance_p->config.cheksum_mode)
        reg_val |= FGMAC_CONF_IPC;
    else
        reg_val &= ~FGMAC_CONF_IPC;

    /* 重发DR=1, 重发一次 */
    reg_val |= FGMAC_CONF_DISABLE_RETRY;

    /* 自动 PAD/ CRC 剥线, 全双工模式保留 */
    reg_val |= FGMAC_CONF_ACS;

    /* 后退限制, 全双工模式保留 */
    reg_val |= FGMAC_CONF_BL(0);

    /* 延期检查禁用 */
    reg_val &= ~FGMAC_CONF_DC;

    /* 使能类型帧的CRC剥离、禁用看门狗WD、禁用Jabber JD、帧突发启用BE、不能自接收DO(全双工保留)*/
    reg_val |= (FGMAC_CONF_CST | FGMAC_CONF_WD | FGMAC_CONF_JD | FGMAC_CONF_BE | FGMAC_CONF_DO);

    FGMAC_WRITE_REG32(base_addr, FGMAC_CONF_OFFSET, reg_val);

    /********gmac filter reg init*********/
    /* Set the RA bit according to ETH ReceiveAll value */
    /* Set the SAF and SAIF bits according to ETH SourceAddrFilter value */
    /* Set the PCF bit according to ETH PassControlFrames value */
    /* Set the DBF bit according to ETH BroadcastFramesReception value */
    /* Set the DAIF bit according to ETH DestinationAddrFilter value */
    /* Set the PR bit according to ETH PromiscuousMode value */
    /* Set the PM, HMC and HPF bits according to ETH MulticastFramesFilter value */
    /* Set the HUC and HPF bits according to ETH UnicastFramesFilter value */

    /* MAC帧过滤寄存器 */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_FRAME_FILTER_OFFSET);

    /* 全部接收RA */
    reg_val |= FGMAC_FRAME_FILTER_RA;

    /* 通过控制帧PCF 10b */
    reg_val |= FGMAC_FRAME_FILTER_PCF(2);

    /* 失能禁用广播帧DBF */
    reg_val &= ~FGMAC_FRAME_FILTER_DBF;

    /* 失能通过所有多播PM */
    reg_val &= ~FGMAC_FRAME_FILTER_PM;

    /* 失能目的地址反向过滤DAIF */
    reg_val &= ~FGMAC_FRAME_FILTER_DAIF;

    /* 失能哈希单播PR */
    reg_val &= ~FGMAC_FRAME_FILTER_PR;
    FGMAC_WRITE_REG32(base_addr, FGMAC_FRAME_FILTER_OFFSET, reg_val);

    /********hash reg*********/
    FGMAC_WRITE_REG32(base_addr, FGMAC_HASH_HIGH_OFFSET, 0x0);
    FGMAC_WRITE_REG32(base_addr, FGMAC_HASH_LOW_OFFSET, 0x0);

    /********gmac flow ctrl reg init*********/

    /* Set the PT bit according to ETH PauseTime value */
    /* Set the DZPQ bit according to ETH ZeroQuantaPause value */
    /* Set the PLT bit according to ETH PauseLowThreshold value */
    /* Set the UP bit according to ETH UnicastPauseFrameDetect value */
    /* Set the RFE bit according to ETH ReceiveFlowControl value */
    /* Set the TFE bit according to ETH TransmitFlowControl value */
    reg_val = FGMAC_READ_REG32(base_addr, FGMAC_FLOW_CTRL_OFFSET);
    /* 禁用自动零暂停帧生成DZPQ */
    reg_val |= FGMAC_FLOW_DZPQ;

    /* 暂停低阈值PLT */
    reg_val |= FGMAC_FLOW_PLT(0);

    /* 禁用接收流控制RFE */
    reg_val &= ~FGMAC_FLOW_RFE;

    /* 禁用传输流控制TFE */
    reg_val &= ~FGMAC_FLOW_TFE;
    FGMAC_WRITE_REG32(base_addr, FGMAC_FLOW_CTRL_OFFSET, reg_val);

    /********vlan tag reg*********/
    /* Set the ETV bit according to ETH VLANTagComparison value */
    /* Set the VL bit according to ETH VLANTagIdentifier value */
    /* 接收帧的 VLAN 标记标识符 VL */
    reg_val = FGMAC_VLAN_TAG_VL(0);
    /* 设置VLAN 标记比较的位数ETV  1-12bit 0-16bit */
    reg_val &= ~FGMAC_VLAN_TAG_ETV;
    FGMAC_WRITE_REG32(base_addr, FGMAC_VLAN_TAG_OFFSET, reg_val);

    return ret;
}

static FError FGmacDmaConfigure(FGmac *instance_p)
{
    FASSERT(instance_p);
    u32 reg_val;
    FError ret = FGMAC_SUCCESS;
    uintptr base_addr = instance_p->config.base_addr;

    /********DMA总线模式寄存器*********/
    /* Set the AAL bit according to ETH AddressAlignedBeats value */
    /* Set the FB bit according to ETH FixedBurst value */
    /* Set the RPBL and 4*PBL bits according to ETH RxDMABurstLength value */
    /* Set the PBL and 4*PBL bits according to ETH TxDMABurstLength value */
    /* Set the Enhanced DMA descriptors bit according to ETH EnhancedDescriptorFormat value*/
    /* Set the DSL bit according to ETH DesciptorSkipLength value */
    /* Set the PR and DA bits according to ETH DMAArbitration value */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_DMA_BUS_MODE_OFFSET);
    reg_val |= FGMAC_DMA_BUS_AAL;
    /* 使用单独的 PBL USP */
    reg_val |= FGMAC_DMA_BUS_USP;
    /* RxDMA PBL RPBL */
    reg_val |= FGMAC_DMA_BUS_RPBL(32);

    /* 固定突发 FB */
    reg_val |= FGMAC_DMA_BUS_FB;

    /* 控制 RxDMA 和 TxDMA 之间的加权循环仲裁中的优先级比率 PR */
    reg_val |= FGMAC_DMA_BUS_PR(0);

    /* 可编程突发长度 PBL */
    reg_val |= FGMAC_DMA_BUS_PBL(32);

    /* 交替描述表大小 ATDS */
    reg_val |= FGMAC_DMA_BUS_ATDS;


    FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_BUS_MODE_OFFSET, reg_val);

    /* dma set bus mode */
    // FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_BUS_MODE_OFFSET, FGMAC_DMA_BUS_INIT);

    /********DMA操作模式寄存器*********/
    /* Set the DT bit according to ETH DropTCPIPChecksumErrorFrame value */
    /* Set the RSF bit according to ETH ReceiveStoreForward value */
    /* Set the DFF bit according to ETH FlushReceivedFrame value */
    /* Set the TSF bit according to ETH TransmitStoreForward value */
    /* Set the TTC bit according to ETH TransmitThresholdControl value */
    /* Set the FEF bit according to ETH ForwardErrorFrames value */
    /* Set the FUF bit according to ETH ForwardUndersizedGoodFrames value */
    /* Set the RTC bit according to ETH ReceiveThresholdControl value */
    /* Set the OSF bit according to ETH SecondFrameOperate value */
    reg_val  = FGMAC_READ_REG32(base_addr, FGMAC_DMA_OP_OFFSET);
    reg_val &= FGMAC_DMA_OP_CLEAR_MASK;
    /* 丢弃 TCP / IP 校验和错误帧 DT */
    reg_val &= ~FGMAC_DMA_OP_DT;

    /* 接收存储转发 RSF */
    reg_val |= FGMAC_DMA_OP_RSF;

    /* 刷新正在接收的帧 DFF */
    reg_val &= ~FGMAC_DMA_OP_DFF;

    /* 发送存储和转发 TSF */
    reg_val |= FGMAC_DMA_OP_TSF;

    /* 传输阈值控制 TTC */
    reg_val |= FGMAC_DMA_OP_TTC(7);

    /* 在第二帧上操作 OSF */
    reg_val |= FGMAC_DMA_OP_OSF;
    FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_OP_OFFSET, reg_val);

    /* 刷新DMA发送FIFO FTF */
    ret = FGmacFlushTxFifo(base_addr, FGMAC_RETRY_TIMES);

    if (FGMAC_SUCCESS != ret)
    {
        printf("FGmac Flush Failed\r\n");
    }

    /* AXI 突发长度 BLEN 16,8,4 */
    reg_val = (FGMAC_DMA_AXI_BUS_MOD_BLEN16 | FGMAC_DMA_AXI_BUS_MOD_BLEN8 | FGMAC_DMA_AXI_BUS_MOD_BLEN4);
    FGMAC_WRITE_REG32(base_addr, FGMAC_DMA_AXI_BUS_MOD_OFFSET, reg_val);

    return ret;
}