1 /*
2  * Copyright (c) 2023-2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_TSW_DRV_H
9 #define HPM_TSW_DRV_H
10 
11 /*---------------------------------------------------------------------
12  * Includes
13  *--------------------------------------------------------------------*/
14 #include "hpm_common.h"
15 #include "hpm_soc_feature.h"
16 #include "hpm_tsw_regs.h"
17 
18 /**
19  * @brief TSW driver APIs
20  * @defgroup tsw_interface TSW driver APIs
21  * @ingroup communication_interfaces
22  * @{
23  */
24 
25 /*---------------------------------------------------------------------
26  *  Macro Constant Declarations
27  *-------------------------------------------------------------------*/
28 #define MAC_LO(mac) (uint32_t)(mac[0] | (mac[1] << 8) | (mac[2] << 16) | (mac[3] << 24))
29 #define MAC_HI(mac) (uint32_t)(mac[4] | (mac[5] << 8))
30 
31 #define MAC_MDIO_CTRL_OP_WR (0x01)
32 #define MAC_MDIO_CTRL_OP_RD (0x02)
33 
34 /*---------------------------------------------------------------------
35  *  Typedef Struct Declarations
36  *-------------------------------------------------------------------*/
37 typedef struct {
38     union {
39         uint32_t tx_hdr0;
40         struct {
41             uint32_t dest_port: 8; /**< dest port */
42             uint32_t          : 8; /**< reserved */
43             uint32_t queue    : 3; /**< the priority queue for TSW TX */
44             uint32_t utag     : 3; /**< TSW-EP TX user sideband information */
45             uint32_t          : 6; /**< reserved */
46             uint32_t htype    : 4; /**< header type */
47         } tx_hdr0_bm;
48     };
49 
50     union {
51         uint32_t tx_hdr1;
52         struct {
53             uint32_t cb: 32; /**< CB field. Optionally used for external stream identification */
54         } tx_hdr1_bm;
55     };
56 
57     uint32_t tx_hdr2; /**< reserved */
58     uint32_t tx_hdr3; /**< reserved */
59 } tx_hdr_desc_t;
60 
61 /*---------------------------------------------------------------------
62  *  Typedef Enum Declarations
63  *-------------------------------------------------------------------*/
64 typedef enum {
65   tsw_port_speed_10mbps   = 2,
66   tsw_port_speed_100mbps  = 3,
67   tsw_port_speed_1000mbps = 0
68 } tsw_port_speed_t;
69 
70 typedef enum {
71   tsw_port_phy_itf_mii   = 0,
72   tsw_port_phy_itf_rmii  = 4,
73   tsw_port_phy_itf_rgmii = 1
74 } tsw_port_phy_itf_t;
75 
76 typedef enum {
77   tsw_dst_port_null = 0,
78   tsw_dst_port_cpu  = 1 << 0,
79   tsw_dst_port_1    = 1 << 1,
80   tsw_dst_port_2    = 1 << 2,
81   tsw_dst_port_3    = 1 << 3,
82 } tsw_dst_t;
83 
84 typedef enum {
85   tsw_mac_mode_mii = 0,
86   tsw_mac_mode_gmii
87 } tsw_mac_mode_t;
88 
89 #if defined __cplusplus
90 extern "C" {
91 #endif /* __cplusplus */
92 /*---------------------------------------------------------------------
93  * Exported Functions
94  *-------------------------------------------------------------------*/
95 /**
96  * @brief Send a frame from CPU port
97  *
98  * @param[in] ptr    TSW peripheral base address
99  * @param[in] length Frame length
100  * @param[in] id     Frame index
101  * @return           Result of the transmission
102  */
103 hpm_stat_t tsw_send(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
104 
105 /**
106  * @brief Setup a receive description
107  *
108  * @param[in] ptr    TSW peripheral base address
109  * @param[in] buffer Pointer to the specified receive buffer
110  * @param[in] length Frame length
111  * @param[in] id     Frame index
112  * @return           Result of the setup of a receive description
113  */
114 hpm_stat_t tsw_recv_setup(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
115 
116 /**
117  * @brief Receive a frame
118  *
119  * @param[in] ptr    TSW peripheral base address
120  * @param[in] buffer Pointer to the specified receive buffer
121  * @param[in] length Buffer length
122  * @param[in] id     Frame index
123  * @return           Result of the received frame
124  */
125 uint32_t tsw_recv(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
126 
127 /**
128  * @brief Lookup Bypass Setting
129  *
130  * @param[in] ptr      TSW peripheral base address
131  * @param[in] dst_port Destination port number
132  */
133 void tsw_mac_lookup_bypass(TSW_Type *ptr, uint8_t dst_port);
134 
135 /**
136  * @brief CAM VLAN Setting
137  *
138  * @param[in] ptr      TSW peripheral base address
139  * @param[in] dst_port Destination port number
140  */
141 void tsw_set_cam_vlan_port(TSW_Type *ptr);
142 
143 /**
144  * @brief MDIO Interface Config
145  *
146  * @param[in] ptr     TSW peripheral base address
147  * @param[in] port    TSW port number
148  * @param[in] clk_div TSW clock division
149  * @return            Result of MDIO interface config
150  */
151 hpm_stat_t tsw_ep_set_mdio_config(TSW_Type *ptr, uint8_t port, uint8_t clk_div);
152 
153 /**
154  * @brief MDIO Read
155  *
156  * @param[in] ptr      TSW peripheral base address
157  * @param[in] port     TSW port number
158  * @param[in] phy_addr TSW clock division
159  * @param[in] reg_addr PHY register address
160  * @param[out] data    Pointer to data memory
161  * @return             Result of MDIO read
162  */
163 hpm_stat_t tsw_ep_mdio_read(TSW_Type *ptr, uint8_t port, uint32_t phy_addr, uint32_t reg_addr, uint16_t *data);
164 
165 /**
166  * @brief MDIO Write
167  *
168  * @param[in] ptr      TSW peripheral base address
169  * @param[in] port     TSW port number
170  * @param[in] phy_addr TSW clock division
171  * @param[in] reg_addr PHY register address
172  * @param[in] data     Data value
173  * @return             Result of MDIO write
174  */
175 hpm_stat_t tsw_ep_mdio_write(TSW_Type *ptr, uint8_t port, uint32_t phy_addr, uint32_t reg_addr, uint16_t data);
176 
177 /**
178  * @brief Enable MAC Controller
179  *
180  * @param[in] ptr      TSW peripheral base address
181  * @param[in] port     TSW port number
182  * @param[in] mac_type MAC type 0:EMAC/1:PMAC
183  * @param[in] enable   Enable MAC Controller: Set true to enable; Set false to disable
184  * @return             Result of controlling MAC controller
185  */
186 hpm_stat_t tsw_ep_enable_mac_ctrl(TSW_Type *ptr, uint8_t port, uint8_t mac_type, bool enable);
187 
188 /**
189  * @brief Set MAC Address
190  *
191  * @param[in] ptr      TSW peripheral base address
192  * @param[in] port     TSW port number
193  * @param[in] mac_addr Pointer to MAC address
194  * @param[in] promisc  Promiscuous Mode: Set true to enable; set false to disable
195  * @return             Result of setting MAC address
196  */
197 hpm_stat_t tsw_ep_set_mac_addr(TSW_Type *ptr, uint8_t port, uint8_t *mac_addr, bool promisc);
198 
199 /**
200  * @brief Set MAC Mode
201  *
202  * @param[in] ptr      TSW peripheral base address
203  * @param[in] port     TSW port number
204  * @param[in] mac_addr Pointer to MAC address
205  * @param[in] promisc  Promiscuous Mode: Set true to enable; set false to disable
206  * @return             Result of setting MAC address
207  */
208 hpm_stat_t tsw_ep_set_mac_mode(TSW_Type *ptr, uint8_t port, uint8_t gmii);
209 
210 /**
211  * @brief Set Port GPR
212  *
213  * @param[in] ptr      TSW peripheral base address
214  * @param[in] port     TSW port number
215  * @param[in] speed    Pointer to MAC address
216  * @param[in] itf      Promiscuous Mode: Set true to enable; set false to disable
217  * @param[in] tx_dly   Tx delay
218  * @param[in] rx_dlay  Rx delay
219  */
220 void tsw_port_gpr(TSW_Type *ptr, uint8_t port, uint8_t speed, uint8_t itf, uint8_t tx_dly, uint8_t rx_dly);
221 
222 /**
223  * @brief Set Internal Frame Action
224  *
225  * @param[in] ptr       TSW peripheral base address
226  * @param[in] dest_port Destination port number
227  */
228 void tsw_set_internal_frame_action(TSW_Type *ptr, uint8_t dest_port);
229 
230 /**
231  * @brief Set Broadcast Frame Action
232  *
233  * @param[in] ptr       TSW peripheral base address
234  * @param[in] dest_port Destination port number
235  */
236 void tsw_set_broadcast_frame_action(TSW_Type *ptr, uint8_t dest_port);
237 
238 /**
239  * @brief Set Unknow Frame Action
240  *
241  * @param[in] ptr       TSW peripheral base address
242  * @param[in] dest_port Destination port number
243  */
244 void tsw_set_unknown_frame_action(TSW_Type *ptr, uint8_t dest_port);
245 
246 /**
247  * @brief Set Lookup Table
248  *
249  * @param[in] ptr       TSW peripheral base address
250  * @param[in] entry_num Entry number
251  * @param[in] dest_port Destination port number
252  * @param[in] dest_mac  Destination MAC address
253  */
254 void tsw_set_lookup_table(TSW_Type *ptr, uint16_t entry_num, uint8_t dest_port, uint64_t dest_mac);
255 
256 /**
257  * @brief Clear CAM
258  *
259  * @param[in] ptr       TSW peripheral base address
260  */
261 void tsw_clear_cam(TSW_Type *ptr);
262 
263 /**
264  * @brief Enable RXFIFO to store and forward mode
265  *
266  * @param[in] ptr       TSW peripheral base address
267  * @param[in] port      TSW port number
268  */
269 void tsw_enable_store_forward_mode(TSW_Type *ptr, uint8_t port);
270 
271 /**
272  * @brief Disable RXFIFO to store and forward mode
273  *
274  * @param[in] ptr       TSW peripheral base address
275  * @param[in] port      TSW port number
276  */
277 void tsw_disable_store_forward_mode(TSW_Type *ptr, uint8_t port);
278 
279 #if defined __cplusplus
280 }
281 #endif /* __cplusplus */
282 /** @} */
283 #endif /* HPM_TSW_DRV_H */
284