1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Email: opensource_embedded@phytium.com.cn
7  *
8  * Change Logs:
9  * Date        Author       Notes
10  * 2025-05-28  huangjin     first commit
11  */
12 #ifndef __DRV_XMAC_MSG_H__
13 #define __DRV_XMAC_MSG_H__
14 #include <rtthread.h>
15 #include <rtdevice.h>
16 
17 #ifdef BSP_USING_ETH_MSG
18 
19 #include <netif/ethernetif.h>
20 
21 #include "fxmac_msg.h"
22 #include "fkernel.h"
23 #include "ferror_code.h"
24 #include "fassert.h"
25 #include "fxmac_msg_bdring.h"
26 #include "eth_ieee_reg.h"
27 #include "fcpu_info.h"
28 #include "fxmac_msg_phy.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #define RTTHREAD_XMAC_MSG_INIT_ERROR        FT_CODE_ERR(ErrModPort, 0, 0x1)
35 #define RTTHREAD_XMAC_MSG_PARAM_ERROR       FT_CODE_ERR(ErrModPort, 0, 0x2)
36 #define RTTHREAD_XMAC_MSG_NO_VALID_SPACE    FT_CODE_ERR(ErrModPort, 0, 0x3)
37 
38 #define FXMAC_MSG_RX_BDSPACE_LENGTH    0x20000 /* default set 128KB*/
39 #define FXMAC_MSG_TX_BDSPACE_LENGTH    0x20000 /* default set 128KB*/
40 
41 #define FXMAC_MSG_RX_PBUFS_LENGTH       64
42 #define FXMAC_MSG_TX_PBUFS_LENGTH       64
43 
44 #define FXMAC_MSG_MAX_HARDWARE_ADDRESS_LENGTH 6
45 
46 #define XMAC_MSG_PHY_RESET_ENABLE 1
47 #define XMAC_MSG_PHY_RESET_DISABLE 0
48 
49 /* configuration */
50 #define FXMAC_MSG_OS_CONFIG_JUMBO  BIT(0)
51 #define FXMAC_MSG_OS_CONFIG_MULTICAST_ADDRESS_FILITER  BIT(1) /* Allow multicast address filtering  */
52 #define FXMAC_MSG_OS_CONFIG_COPY_ALL_FRAMES BIT(2) /* enable copy all frames */
53 #define FXMAC_MSG_OS_CONFIG_CLOSE_FCS_CHECK BIT(3) /* close fcs check */
54 #define FXMAC_MSG_OS_CONFIG_RX_POLL_RECV BIT(4)  /* select poll mode */
55 #define FXMAC_MSG_OS_CONFIG_UNICAST_ADDRESS_FILITER BIT(5) /* Allow unicast address filtering  */
56 /* Phy */
57 #define FXMAC_MSG_PHY_SPEED_10M    10
58 #define FXMAC_MSG_PHY_SPEED_100M    100
59 #define FXMAC_MSG_PHY_SPEED_1000M    1000
60 
61 #define FXMAC_MSG_PHY_HALF_DUPLEX   0
62 #define FXMAC_MSG_PHY_FULL_DUPLEX   1
63 
64 #define MAX_FRAME_SIZE_JUMBO (FXMAC_MSG_MTU_JUMBO + FXMAC_MSG_HDR_SIZE + FXMAC_MSG_TRL_SIZE)
65 
66 /* Byte alignment of BDs */
67 #define BD_ALIGNMENT (FXMAC_MSG_DMABD_MINIMUM_ALIGNMENT*2)
68 
69 /*  frame queue */
70 #define PQ_QUEUE_SIZE 4096
71 
72 #define LINK_THREAD_STACK_LENGTH 0x20400
73 
74 
75 typedef struct
76 {
77     uintptr data[PQ_QUEUE_SIZE];
78     int head, tail, len;
79 } PqQueue;
80 
81 typedef enum
82 {
83     FXMAC_MSG_OS_INTERFACE_SGMII = 0,
84     FXMAC_MSG_OS_INTERFACE_RMII,
85     FXMAC_MSG_OS_INTERFACE_RGMII,
86     FXMAC_MSG_OS_INTERFACE_LENGTH
87 } FXmacMsgRtThreadInterface;
88 
89 
90 typedef struct
91 {
92     u8 rx_bdspace[FXMAC_MSG_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 接收bd 缓冲区 */
93     u8 tx_bdspace[FXMAC_MSG_RX_BDSPACE_LENGTH] __attribute__((aligned(128))); /* 发送bd 缓冲区 */
94 
95     uintptr rx_pbufs_storage[FXMAC_MSG_RX_PBUFS_LENGTH];
96     uintptr tx_pbufs_storage[FXMAC_MSG_TX_PBUFS_LENGTH];
97 
98 } FXmacMsgNetifBuffer;
99 
100 typedef struct
101 {
102     u32 instance_id;
103     FXmacMsgRtThreadInterface interface;
104     u32 autonegotiation; /* 1 is autonegotiation ,0 is manually set */
105     u32 phy_speed;  /* FXMAC_PHY_SPEED_XXX */
106     u32 phy_duplex; /* FXMAC_PHY_XXX_DUPLEX */
107 } FXmacMsgOsControl;
108 
109 
110 typedef struct
111 {
112     struct eth_device parent; /* inherit from ethernet device */
113 
114     FXmacMsgCtrl instance; /* Xmac controller */
115     FXmacMsgOsControl mac_config;
116 
117     FXmacMsgNetifBuffer buffer; /* DMA buffer */
118 
119     /* queue to store overflow packets */
120     PqQueue recv_q;
121     PqQueue send_q;
122 
123     /* configuration */
124     u32 config;
125     u32 is_link_up;
126     rt_uint8_t hwaddr[FXMAC_MSG_MAX_HARDWARE_ADDRESS_LENGTH]; /* MAC address */
127 
128     struct rt_thread _link_thread; /* link detect thread */
129     rt_uint8_t _link_thread_stack[LINK_THREAD_STACK_LENGTH];/* link detect thread stack*/
130 } FXmacMsgOs;
131 
132 enum lwip_port_link_status
133 {
134     ETH_LINK_UNDEFINED = 0,
135     ETH_LINK_UP,
136     ETH_LINK_DOWN,
137     ETH_LINK_NEGOTIATING
138 };
139 
140 
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif // !
147 
148 #endif
149