1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*******************************************************************************
8  * NOTICE
9  * The hal is not public api, don't use in application code.
10  * See readme.md in hal/include/hal/readme.md
11  ******************************************************************************/
12 
13 // The LL layer for ESP32 eMAC register operations
14 
15 #pragma once
16 
17 #include <stdint.h>
18 #include "hal/misc.h"
19 #include "hal/eth_types.h"
20 #include "soc/emac_dma_struct.h"
21 #include "soc/emac_mac_struct.h"
22 #include "soc/emac_ext_struct.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /* Register configuration */
29 #define EMAC_LL_INTERFRAME_GAP_96BIT (0)
30 #define EMAC_LL_INTERFRAME_GAP_88BIT (1)
31 #define EMAC_LL_INTERFRAME_GAP_80BIT (2)
32 #define EMAC_LL_INTERFRAME_GAP_72BIT (3)
33 #define EMAC_LL_INTERFRAME_GAP_64BIT (4)
34 #define EMAC_LL_INTERFRAME_GAP_56BIT (5)
35 #define EMAC_LL_INTERFRAME_GAP_48BIT (6)
36 #define EMAC_LL_INTERFRAME_GAP_40BIT (7)
37 
38 #define EMAC_LL_BACKOFF_LIMIT_10 (0)
39 #define EMAC_LL_BACKOFF_LIMIT_8 (1)
40 #define EMAC_LL_BACKOFF_LIMIT_4 (2)
41 #define EMAC_LL_BACKOFF_LIMIT_1 (3)
42 
43 #define EMAC_LL_PREAMBLE_LENGTH_7 (0)
44 #define EMAC_LL_PREAMBLE_LENGTH_5 (1)
45 #define EMAC_LL_PREAMBLE_LENGTH_3 (2)
46 
47 #define EMAC_LL_SOURCE_ADDR_FILTER_DISABLE (0)
48 #define EMAC_LL_SOURCE_ADDR_FILTER_NORMAL (2)
49 #define EMAC_LL_SOURCE_ADDR_FILTER_INVERSE (3)
50 
51 #define EMAC_LL_CONTROL_FRAME_BLOCKALL (0)
52 #define EMAC_LL_CONTROL_FRAME_FORWARDALL_PAUSE (1)
53 #define EMAC_LL_CONTROL_FRAME_FORWARDALL (2)
54 #define EMAC_LL_CONTROL_FRAME_FORWARDFILT (3)
55 
56 #define EMAC_LL_PAUSE_TIME 0x1648
57 
58 #define EMAC_LL_PAUSE_LOW_THRESHOLD_MINUS_4 (0)
59 #define EMAC_LL_PAUSE_LOW_THRESHOLD_MINUS_28 (1)
60 #define EMAC_LL_PAUSE_LOW_THRESHOLD_MINUS_144 (2)
61 #define EMAC_LL_PAUSE_LOW_THRESHOLD_MINUS_256 (3)
62 
63 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_64 (0)
64 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_128 (1)
65 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_192 (2)
66 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_256 (3)
67 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_40 (4)
68 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_32 (5)
69 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_24 (6)
70 #define EMAC_LL_TRANSMIT_THRESHOLD_CONTROL_16 (7)
71 
72 #define EMAC_LL_RECEIVE_THRESHOLD_CONTROL_64 (0)
73 #define EMAC_LL_RECEIVE_THRESHOLD_CONTROL_32 (1)
74 #define EMAC_LL_RECEIVE_THRESHOLD_CONTROL_96 (2)
75 #define EMAC_LL_RECEIVE_THRESHOLD_CONTROL_128 (3)
76 
77 #define EMAC_LL_DMA_BURST_LENGTH_1BEAT (1)
78 #define EMAC_LL_DMA_BURST_LENGTH_2BEAT (2)
79 #define EMAC_LL_DMA_BURST_LENGTH_4BEAT (4)
80 #define EMAC_LL_DMA_BURST_LENGTH_8BEAT (8)
81 #define EMAC_LL_DMA_BURST_LENGTH_16BEAT (16)
82 #define EMAC_LL_DMA_BURST_LENGTH_32BEAT (32)
83 
84 #define EMAC_LL_DMA_ARBITRATION_ROUNDROBIN_RXTX_1_1 (0)
85 #define EMAC_LL_DMA_ARBITRATION_ROUNDROBIN_RXTX_2_1 (1)
86 #define EMAC_LL_DMA_ARBITRATION_ROUNDROBIN_RXTX_3_1 (2)
87 #define EMAC_LL_DMA_ARBITRATION_ROUNDROBIN_RXTX_4_1 (3)
88 
89 /* PTP register bits */
90 #define EMAC_LL_DMAPTPRXDESC_PTPMT_SYNC 0x00000100U                      /* SYNC message (all clock types) */
91 #define EMAC_LL_DMAPTPRXDESC_PTPMT_FOLLOWUP 0x00000200U                  /* FollowUp message (all clock types) */
92 #define EMAC_LL_DMAPTPRXDESC_PTPMT_DELAYREQ 0x00000300U                  /* DelayReq message (all clock types) */
93 #define EMAC_LL_DMAPTPRXDESC_PTPMT_DELAYRESP 0x00000400U                 /* DelayResp message (all clock types) */
94 #define EMAC_LL_DMAPTPRXDESC_PTPMT_PDELAYREQ_ANNOUNCE 0x00000500U        /* PdelayReq message (peer-to-peer transparent clock) or Announce message (Ordinary or Boundary clock) */
95 #define EMAC_LL_DMAPTPRXDESC_PTPMT_PDELAYRESP_MANAG 0x00000600U          /* PdelayResp message (peer-to-peer transparent clock) or Management message (Ordinary or Boundary clock)  */
96 #define EMAC_LL_DMAPTPRXDESC_PTPMT_PDELAYRESPFOLLOWUP_SIGNAL 0x00000700U /* PdelayRespFollowUp message (peer-to-peer transparent clock) or Signaling message (Ordinary or Boundary clock) */
97 
98 #define EMAC_LL_DMAPTPRXDESC_IPPT_UDP 0x00000001U  /* UDP payload encapsulated in the IP datagram */
99 #define EMAC_LL_DMAPTPRXDESC_IPPT_TCP 0x00000002U  /* TCP payload encapsulated in the IP datagram */
100 #define EMAC_LL_DMAPTPRXDESC_IPPT_ICMP 0x00000003U /* ICMP payload encapsulated in the IP datagram */
101 
102 #define EMAC_LL_DMADESC_OWNER_CPU  (0)
103 #define EMAC_LL_DMADESC_OWNER_DMA  (1)
104 
105 /* Interrupt flags (referring to dmastatus register in emac_dma_struct.h) */
106 #define EMAC_LL_DMA_TRANSMIT_FINISH_INTR               0x00000001U
107 #define EMAC_LL_DMA_TRANSMIT_STOP_INTR                 0x00000002U
108 #define EMAC_LL_DMA_TRANSMIT_BUFF_UNAVAILABLE_INTR     0x00000004U
109 #define EMAC_LL_DMA_TRANSMIT_TIMEOUT_INTR              0x00000008U
110 #define EMAC_LL_DMA_RECEIVE_OVERFLOW_INTR              0x00000010U
111 #define EMAC_LL_DMA_TRANSMIT_UNDERFLOW_INTR            0x00000020U
112 #define EMAC_LL_DMA_RECEIVE_FINISH_INTR                0x00000040U
113 #define EMAC_LL_DMA_RECEIVE_BUFF_UNAVAILABLE_INTR      0x00000080U
114 #define EMAC_LL_DMA_RECEIVE_STOP_INTR                  0x00000100U
115 #define EMAC_LL_DMA_RECEIVE_TIMEOUT_INTR               0x00000200U
116 #define EMAC_LL_DMA_TRANSMIT_FIRST_BYTE_INTR           0x00000400U
117 #define EMAC_LL_DMA_FATAL_BUS_ERROR_INRT               0x00001000U
118 #define EMAC_LL_DMA_RECEIVE_FIRST_BYTE_INTR            0x00002000U
119 #define EMAC_LL_DMA_ABNORMAL_INTR_SUMMARY              0x00004000U
120 #define EMAC_LL_DMA_NORMAL_INTR_SUMMARY                0x00008000U
121 #define EMAC_LL_DMA_POWER_MANAGE_INTR                  0x10000000U
122 #define EMAC_LL_DMA_TIMESTAMP_TRIGGER_INTR             0x20000000U
123 
124 /* Interrupt enable (referring to dmain_en register in emac_dma_struct.h) */
125 #define EMAC_LL_INTR_TRANSMIT_ENABLE                    0x00000001U
126 #define EMAC_LL_INTR_TRANSMIT_STOP_ENABLE               0x00000002U
127 #define EMAC_LL_INTR_TRANSMIT_BUFF_UNAVAILABLE_ENABLE   0x00000004U
128 #define EMAC_LL_INTR_TRANSMIT_TIMEOUT_ENABLE            0x00000008U
129 #define EMAC_LL_INTR_OVERFLOW_ENABLE                    0x00000010U
130 #define EMAC_LL_INTR_UNDERFLOW_ENABLE                   0x00000020U
131 #define EMAC_LL_INTR_RECEIVE_ENABLE                     0x00000040U
132 #define EMAC_LL_INTR_RECEIVE_BUFF_UNAVAILABLE_ENABLE    0x00000080U
133 #define EMAC_LL_INTR_RECEIVE_STOP_ENABLE                0x00000100U
134 #define EMAC_LL_INTR_RECEIVE_TIMEOUT_ENABLE             0x00000200U
135 #define EMAC_LL_INTR_TRANSMIT_FIRST_BYTE_ENABLE         0x00000400U
136 #define EMAC_LL_INTR_FATAL_BUS_ERR_ENABLE               0x00002000U
137 #define EMAC_LL_INTR_RECEIVE_FIRST_BYTE_ENABLE          0x00004000U
138 #define EMAC_LL_INTR_ABNORMAL_SUMMARY_ENABLE            0x00008000U
139 #define EMAC_LL_INTR_NORMAL_SUMMARY_ENABLE              0x00010000U
140 
141 /* Enable needed interrupts (recv/recv_buf_unavailabal/normal must be enabled to make eth work) */
142 #define EMAC_LL_CONFIG_ENABLE_INTR_MASK    (EMAC_LL_INTR_RECEIVE_ENABLE | EMAC_LL_INTR_NORMAL_SUMMARY_ENABLE)
143 
144 /************** Start of mac regs operation ********************/
145 /* emacgmiiaddr */
emac_ll_set_csr_clock_division(emac_mac_dev_t * mac_regs,uint32_t div_mode)146 static inline void emac_ll_set_csr_clock_division(emac_mac_dev_t *mac_regs, uint32_t div_mode)
147 {
148     mac_regs->emacgmiiaddr.miicsrclk = div_mode;
149 }
150 
emac_ll_is_mii_busy(emac_mac_dev_t * mac_regs)151 static inline bool emac_ll_is_mii_busy(emac_mac_dev_t *mac_regs)
152 {
153     return mac_regs->emacgmiiaddr.miibusy ? true : false;
154 }
155 
emac_ll_set_phy_addr(emac_mac_dev_t * mac_regs,uint32_t addr)156 static inline void emac_ll_set_phy_addr(emac_mac_dev_t *mac_regs, uint32_t addr)
157 {
158     mac_regs->emacgmiiaddr.miidev = addr;
159 }
160 
emac_ll_set_phy_reg(emac_mac_dev_t * mac_regs,uint32_t reg)161 static inline void emac_ll_set_phy_reg(emac_mac_dev_t *mac_regs, uint32_t reg)
162 {
163     mac_regs->emacgmiiaddr.miireg = reg;
164 }
165 
emac_ll_write_enable(emac_mac_dev_t * mac_regs,bool enable)166 static inline void emac_ll_write_enable(emac_mac_dev_t *mac_regs, bool enable)
167 {
168     mac_regs->emacgmiiaddr.miiwrite = enable;
169 }
170 
emac_ll_set_busy(emac_mac_dev_t * mac_regs,bool busy)171 static inline void emac_ll_set_busy(emac_mac_dev_t *mac_regs, bool busy)
172 {
173     mac_regs->emacgmiiaddr.miibusy = busy ? 1 : 0;
174 }
175 
176 /* gmacconfig */
emac_ll_watchdog_enable(emac_mac_dev_t * mac_regs,bool enable)177 static inline void emac_ll_watchdog_enable(emac_mac_dev_t *mac_regs, bool enable)
178 {
179     mac_regs->gmacconfig.watchdog = !enable;
180 }
181 
emac_ll_jabber_enable(emac_mac_dev_t * mac_regs,bool enable)182 static inline void emac_ll_jabber_enable(emac_mac_dev_t *mac_regs, bool enable)
183 {
184     mac_regs->gmacconfig.jabber = !enable;
185 }
186 
emac_ll_set_inter_frame_gap(emac_mac_dev_t * mac_regs,uint32_t gap)187 static inline void emac_ll_set_inter_frame_gap(emac_mac_dev_t *mac_regs, uint32_t gap)
188 {
189     mac_regs->gmacconfig.interframegap = gap;
190 }
191 
emac_ll_carrier_sense_enable(emac_mac_dev_t * mac_regs,bool enable)192 static inline void emac_ll_carrier_sense_enable(emac_mac_dev_t *mac_regs, bool enable)
193 {
194     mac_regs->gmacconfig.disablecrs = !enable;
195 }
196 
emac_ll_set_port_speed(emac_mac_dev_t * mac_regs,eth_speed_t speed)197 static inline void emac_ll_set_port_speed(emac_mac_dev_t *mac_regs, eth_speed_t speed)
198 {
199     if (speed == ETH_SPEED_10M || speed == ETH_SPEED_100M) {
200         mac_regs->gmacconfig.mii = 1;   // 10_100MBPS
201         mac_regs->gmacconfig.fespeed = speed;
202     } else {
203         mac_regs->gmacconfig.mii = 0;   // 1000MBPS
204     }
205 }
206 
emac_ll_recv_own_enable(emac_mac_dev_t * mac_regs,bool enable)207 static inline void emac_ll_recv_own_enable(emac_mac_dev_t *mac_regs, bool enable)
208 {
209     mac_regs->gmacconfig.rxown = !enable;
210 }
211 
emac_ll_loopback_enable(emac_mac_dev_t * mac_regs,bool enable)212 static inline void emac_ll_loopback_enable(emac_mac_dev_t *mac_regs, bool enable)
213 {
214     mac_regs->gmacconfig.loopback = enable;
215 }
216 
emac_ll_set_duplex(emac_mac_dev_t * mac_regs,eth_duplex_t duplex)217 static inline void emac_ll_set_duplex(emac_mac_dev_t *mac_regs, eth_duplex_t duplex)
218 {
219     mac_regs->gmacconfig.duplex = duplex;
220 }
221 
emac_ll_checksum_offload_mode(emac_mac_dev_t * mac_regs,eth_checksum_t mode)222 static inline void emac_ll_checksum_offload_mode(emac_mac_dev_t *mac_regs, eth_checksum_t mode)
223 {
224     mac_regs->gmacconfig.rxipcoffload = mode;
225 }
226 
emac_ll_retry_enable(emac_mac_dev_t * mac_regs,bool enable)227 static inline void emac_ll_retry_enable(emac_mac_dev_t *mac_regs, bool enable)
228 {
229     mac_regs->gmacconfig.retry = !enable;
230 }
231 
emac_ll_auto_pad_crc_strip_enable(emac_mac_dev_t * mac_regs,bool enable)232 static inline void emac_ll_auto_pad_crc_strip_enable(emac_mac_dev_t *mac_regs, bool enable)
233 {
234     mac_regs->gmacconfig.padcrcstrip = enable;
235 }
236 
emac_ll_set_back_off_limit(emac_mac_dev_t * mac_regs,uint32_t limit)237 static inline void emac_ll_set_back_off_limit(emac_mac_dev_t *mac_regs, uint32_t limit)
238 {
239     mac_regs->gmacconfig.backofflimit = limit;
240 }
241 
emac_ll_deferral_check_enable(emac_mac_dev_t * mac_regs,bool enable)242 static inline void emac_ll_deferral_check_enable(emac_mac_dev_t *mac_regs, bool enable)
243 {
244     mac_regs->gmacconfig.deferralcheck = enable;
245 }
246 
emac_ll_set_preamble_length(emac_mac_dev_t * mac_regs,uint32_t len)247 static inline void emac_ll_set_preamble_length(emac_mac_dev_t *mac_regs, uint32_t len)
248 {
249     mac_regs->gmacconfig.pltf = len;
250 }
251 
emac_ll_transmit_enable(emac_mac_dev_t * mac_regs,bool enable)252 static inline void emac_ll_transmit_enable(emac_mac_dev_t *mac_regs, bool enable)
253 {
254     mac_regs->gmacconfig.tx = enable;
255 }
256 
emac_ll_receive_enable(emac_mac_dev_t * mac_regs,bool enable)257 static inline void emac_ll_receive_enable(emac_mac_dev_t *mac_regs, bool enable)
258 {
259     mac_regs->gmacconfig.rx = enable;
260 }
261 
262 /* gmacff */
emac_ll_receive_all_enable(emac_mac_dev_t * mac_regs,bool enable)263 static inline void emac_ll_receive_all_enable(emac_mac_dev_t *mac_regs, bool enable)
264 {
265     mac_regs->gmacff.receive_all = enable;
266 }
267 
emac_ll_set_src_addr_filter(emac_mac_dev_t * mac_regs,uint32_t filter)268 static inline void emac_ll_set_src_addr_filter(emac_mac_dev_t *mac_regs, uint32_t filter)
269 {
270     mac_regs->gmacff.safe = filter;
271 }
272 
emac_ll_sa_inverse_filter_enable(emac_mac_dev_t * mac_regs,bool enable)273 static inline void emac_ll_sa_inverse_filter_enable(emac_mac_dev_t *mac_regs, bool enable)
274 {
275     mac_regs->gmacff.saif = enable;
276 }
277 
emac_ll_set_pass_ctrl_frame_mode(emac_mac_dev_t * mac_regs,uint32_t mode)278 static inline void emac_ll_set_pass_ctrl_frame_mode(emac_mac_dev_t *mac_regs, uint32_t mode)
279 {
280     mac_regs->gmacff.pcf = mode;
281 }
282 
emac_ll_broadcast_frame_enable(emac_mac_dev_t * mac_regs,bool enable)283 static inline void emac_ll_broadcast_frame_enable(emac_mac_dev_t *mac_regs, bool enable)
284 {
285     mac_regs->gmacff.dbf = !enable;
286 }
287 
emac_ll_pass_all_multicast_enable(emac_mac_dev_t * mac_regs,bool enable)288 static inline void emac_ll_pass_all_multicast_enable(emac_mac_dev_t *mac_regs, bool enable)
289 {
290     mac_regs->gmacff.pam = enable;
291 }
292 
emac_ll_da_inverse_filter_enable(emac_mac_dev_t * mac_regs,bool enable)293 static inline void emac_ll_da_inverse_filter_enable(emac_mac_dev_t *mac_regs, bool enable)
294 {
295     mac_regs->gmacff.daif = enable;
296 }
297 
emac_ll_promiscuous_mode_enable(emac_mac_dev_t * mac_regs,bool enable)298 static inline void emac_ll_promiscuous_mode_enable(emac_mac_dev_t *mac_regs, bool enable)
299 {
300     mac_regs->gmacff.pmode = enable;
301 }
302 
303 /* gmacfc */
emac_ll_set_pause_time(emac_mac_dev_t * mac_regs,uint32_t time)304 static inline void emac_ll_set_pause_time(emac_mac_dev_t *mac_regs, uint32_t time)
305 {
306     HAL_FORCE_MODIFY_U32_REG_FIELD(mac_regs->gmacfc, pause_time, time);
307 }
308 
emac_ll_zero_quanta_pause_enable(emac_mac_dev_t * mac_regs,bool enable)309 static inline void emac_ll_zero_quanta_pause_enable(emac_mac_dev_t *mac_regs, bool enable)
310 {
311     mac_regs->gmacfc.dzpq = !enable;
312 }
313 
emac_ll_set_pause_low_threshold(emac_mac_dev_t * mac_regs,uint32_t threshold)314 static inline void emac_ll_set_pause_low_threshold(emac_mac_dev_t *mac_regs, uint32_t threshold)
315 {
316     mac_regs->gmacfc.plt = threshold;
317 }
318 
emac_ll_unicast_pause_frame_detect_enable(emac_mac_dev_t * mac_regs,bool enable)319 static inline void emac_ll_unicast_pause_frame_detect_enable(emac_mac_dev_t *mac_regs, bool enable)
320 {
321     mac_regs->gmacfc.upfd = enable;
322 }
323 
emac_ll_receive_flow_ctrl_enable(emac_mac_dev_t * mac_regs,bool enable)324 static inline void emac_ll_receive_flow_ctrl_enable(emac_mac_dev_t *mac_regs, bool enable)
325 {
326     mac_regs->gmacfc.rfce = enable;
327 }
328 
emac_ll_transmit_flow_ctrl_enable(emac_mac_dev_t * mac_regs,bool enable)329 static inline void emac_ll_transmit_flow_ctrl_enable(emac_mac_dev_t *mac_regs, bool enable)
330 {
331     mac_regs->gmacfc.tfce = enable;
332 }
333 
emac_ll_clear(emac_mac_dev_t * mac_regs)334 static inline void emac_ll_clear(emac_mac_dev_t *mac_regs)
335 {
336     mac_regs->gmacfc.val = 0;
337 }
338 
339 /* emacdebug */
emac_ll_transmit_frame_ctrl_status(emac_mac_dev_t * mac_regs)340 static inline uint32_t emac_ll_transmit_frame_ctrl_status(emac_mac_dev_t *mac_regs)
341 {
342     return mac_regs->emacdebug.mactfcs;
343 }
344 
emac_ll_receive_read_ctrl_state(emac_mac_dev_t * mac_regs)345 static inline uint32_t emac_ll_receive_read_ctrl_state(emac_mac_dev_t *mac_regs)
346 {
347     return mac_regs->emacdebug.mtlrfrcs;
348 }
349 
350 /* emacmiidata */
emac_ll_set_phy_data(emac_mac_dev_t * mac_regs,uint32_t data)351 static inline void emac_ll_set_phy_data(emac_mac_dev_t *mac_regs, uint32_t data)
352 {
353     HAL_FORCE_MODIFY_U32_REG_FIELD(mac_regs->emacmiidata, mii_data, data);
354 }
355 
emac_ll_get_phy_data(emac_mac_dev_t * mac_regs)356 static inline uint32_t emac_ll_get_phy_data(emac_mac_dev_t *mac_regs)
357 {
358     return HAL_FORCE_READ_U32_REG_FIELD(mac_regs->emacmiidata, mii_data);
359 }
360 
361 /* emacaddr0 */
emac_ll_set_addr(emac_mac_dev_t * mac_regs,const uint8_t * addr)362 static inline void emac_ll_set_addr(emac_mac_dev_t *mac_regs, const uint8_t *addr)
363 {
364     HAL_FORCE_MODIFY_U32_REG_FIELD(mac_regs->emacaddr0high, address0_hi, (addr[5] << 8) | addr[4]);
365     mac_regs->emacaddr0low = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | (addr[0]);
366 }
367 /*************** End of mac regs operation *********************/
368 
369 
370 
371 /************** Start of dma regs operation ********************/
372 /* dmabusmode */
emac_ll_reset(emac_dma_dev_t * dma_regs)373 static inline void emac_ll_reset(emac_dma_dev_t *dma_regs)
374 {
375     dma_regs->dmabusmode.sw_rst = 1;
376 }
377 
emac_ll_is_reset_done(emac_dma_dev_t * dma_regs)378 static inline bool emac_ll_is_reset_done(emac_dma_dev_t *dma_regs)
379 {
380     return dma_regs->dmabusmode.sw_rst ? false : true;
381 }
382 
383 /* dmarxbaseaddr / dmatxbaseaddr */
emac_ll_set_rx_desc_addr(emac_dma_dev_t * dma_regs,uint32_t addr)384 static inline void emac_ll_set_rx_desc_addr(emac_dma_dev_t *dma_regs, uint32_t addr)
385 {
386     dma_regs->dmarxbaseaddr = addr;
387 }
388 
emac_ll_set_tx_desc_addr(emac_dma_dev_t * dma_regs,uint32_t addr)389 static inline void emac_ll_set_tx_desc_addr(emac_dma_dev_t *dma_regs, uint32_t addr)
390 {
391     dma_regs->dmatxbaseaddr = addr;
392 }
393 
394 /* dmaoperation_mode */
emac_ll_drop_tcp_err_frame_enable(emac_dma_dev_t * dma_regs,bool enable)395 static inline void emac_ll_drop_tcp_err_frame_enable(emac_dma_dev_t *dma_regs, bool enable)
396 {
397     dma_regs->dmaoperation_mode.dis_drop_tcpip_err_fram = !enable;
398 }
399 
emac_ll_recv_store_forward_enable(emac_dma_dev_t * dma_regs,bool enable)400 static inline void emac_ll_recv_store_forward_enable(emac_dma_dev_t *dma_regs, bool enable)
401 {
402     dma_regs->dmaoperation_mode.rx_store_forward = enable;
403 }
404 
emac_ll_flush_recv_frame_enable(emac_dma_dev_t * dma_regs,bool enable)405 static inline void emac_ll_flush_recv_frame_enable(emac_dma_dev_t *dma_regs, bool enable)
406 {
407     dma_regs->dmaoperation_mode.dis_flush_recv_frames = !enable;
408 }
409 
emac_ll_trans_store_forward_enable(emac_dma_dev_t * dma_regs,bool enable)410 static inline void emac_ll_trans_store_forward_enable(emac_dma_dev_t *dma_regs, bool enable)
411 {
412     dma_regs->dmaoperation_mode.tx_str_fwd = enable;
413 }
414 
emac_ll_flush_trans_fifo_enable(emac_dma_dev_t * dma_regs,bool enable)415 static inline void emac_ll_flush_trans_fifo_enable(emac_dma_dev_t *dma_regs, bool enable)
416 {
417     dma_regs->dmaoperation_mode.flush_tx_fifo = enable;
418 }
419 
emac_ll_get_flush_trans_fifo(emac_dma_dev_t * dma_regs)420 static inline bool emac_ll_get_flush_trans_fifo(emac_dma_dev_t *dma_regs)
421 {
422     return dma_regs->dmaoperation_mode.flush_tx_fifo;
423 }
424 
emac_ll_set_transmit_threshold(emac_dma_dev_t * dma_regs,uint32_t threshold)425 static inline void emac_ll_set_transmit_threshold(emac_dma_dev_t *dma_regs, uint32_t threshold)
426 {
427     dma_regs->dmaoperation_mode.tx_thresh_ctrl = threshold;
428 }
429 
emac_ll_start_stop_dma_transmit(emac_dma_dev_t * dma_regs,bool enable)430 static inline void emac_ll_start_stop_dma_transmit(emac_dma_dev_t *dma_regs, bool enable)
431 {
432     dma_regs->dmaoperation_mode.start_stop_transmission_command = enable;
433 }
434 
emac_ll_forward_err_frame_enable(emac_dma_dev_t * dma_regs,bool enable)435 static inline void emac_ll_forward_err_frame_enable(emac_dma_dev_t *dma_regs, bool enable)
436 {
437     dma_regs->dmaoperation_mode.fwd_err_frame = enable;
438 }
439 
emac_ll_forward_undersized_good_frame_enable(emac_dma_dev_t * dma_regs,bool enable)440 static inline void emac_ll_forward_undersized_good_frame_enable(emac_dma_dev_t *dma_regs, bool enable)
441 {
442     dma_regs->dmaoperation_mode.fwd_under_gf = enable;
443 }
444 
emac_ll_set_recv_threshold(emac_dma_dev_t * dma_regs,uint32_t threshold)445 static inline void emac_ll_set_recv_threshold(emac_dma_dev_t *dma_regs, uint32_t threshold)
446 {
447     dma_regs->dmaoperation_mode.rx_thresh_ctrl = threshold;
448 }
449 
emac_ll_opt_second_frame_enable(emac_dma_dev_t * dma_regs,bool enable)450 static inline void emac_ll_opt_second_frame_enable(emac_dma_dev_t *dma_regs, bool enable)
451 {
452     dma_regs->dmaoperation_mode.opt_second_frame = enable;
453 }
454 
emac_ll_start_stop_dma_receive(emac_dma_dev_t * dma_regs,bool enable)455 static inline void emac_ll_start_stop_dma_receive(emac_dma_dev_t *dma_regs, bool enable)
456 {
457     dma_regs->dmaoperation_mode.start_stop_rx = enable;
458 }
459 
460 /* dmabusmode */
emac_ll_mixed_burst_enable(emac_dma_dev_t * dma_regs,bool enable)461 static inline void emac_ll_mixed_burst_enable(emac_dma_dev_t *dma_regs, bool enable)
462 {
463     dma_regs->dmabusmode.dmamixedburst = enable;
464 }
465 
emac_ll_addr_align_enable(emac_dma_dev_t * dma_regs,bool enable)466 static inline void emac_ll_addr_align_enable(emac_dma_dev_t *dma_regs, bool enable)
467 {
468     dma_regs->dmabusmode.dmaaddralibea = enable;
469 }
470 
emac_ll_use_separate_pbl_enable(emac_dma_dev_t * dma_regs,bool enable)471 static inline void emac_ll_use_separate_pbl_enable(emac_dma_dev_t *dma_regs, bool enable)
472 {
473     dma_regs->dmabusmode.use_sep_pbl = enable;
474 }
475 
emac_ll_set_rx_dma_pbl(emac_dma_dev_t * dma_regs,uint32_t pbl)476 static inline void emac_ll_set_rx_dma_pbl(emac_dma_dev_t *dma_regs, uint32_t pbl)
477 {
478     dma_regs->dmabusmode.rx_dma_pbl = pbl;
479 }
480 
emac_ll_set_prog_burst_len(emac_dma_dev_t * dma_regs,eth_mac_dma_burst_len_t dma_burst_len)481 static inline void emac_ll_set_prog_burst_len(emac_dma_dev_t *dma_regs, eth_mac_dma_burst_len_t dma_burst_len)
482 {
483     dma_regs->dmabusmode.prog_burst_len =   dma_burst_len == ETH_DMA_BURST_LEN_1 ? EMAC_LL_DMA_BURST_LENGTH_1BEAT :
484                                             dma_burst_len == ETH_DMA_BURST_LEN_2 ? EMAC_LL_DMA_BURST_LENGTH_2BEAT :
485                                             dma_burst_len == ETH_DMA_BURST_LEN_4 ? EMAC_LL_DMA_BURST_LENGTH_4BEAT :
486                                             dma_burst_len == ETH_DMA_BURST_LEN_8 ? EMAC_LL_DMA_BURST_LENGTH_8BEAT :
487                                             dma_burst_len == ETH_DMA_BURST_LEN_16 ? EMAC_LL_DMA_BURST_LENGTH_16BEAT :
488                                             EMAC_LL_DMA_BURST_LENGTH_32BEAT;
489 }
490 
emac_ll_enhance_desc_enable(emac_dma_dev_t * dma_regs,bool enable)491 static inline void emac_ll_enhance_desc_enable(emac_dma_dev_t *dma_regs, bool enable)
492 {
493     dma_regs->dmabusmode.alt_desc_size = enable;
494 }
495 
emac_ll_set_desc_skip_len(emac_dma_dev_t * dma_regs,uint32_t len)496 static inline void emac_ll_set_desc_skip_len(emac_dma_dev_t *dma_regs, uint32_t len)
497 {
498     dma_regs->dmabusmode.desc_skip_len = len;
499 }
500 
emac_ll_fixed_arbitration_enable(emac_dma_dev_t * dma_regs,bool enable)501 static inline void emac_ll_fixed_arbitration_enable(emac_dma_dev_t *dma_regs, bool enable)
502 {
503     dma_regs->dmabusmode.dma_arb_sch = enable;
504 }
505 
emac_ll_set_priority_ratio(emac_dma_dev_t * dma_regs,uint32_t ratio)506 static inline void emac_ll_set_priority_ratio(emac_dma_dev_t *dma_regs, uint32_t ratio)
507 {
508     dma_regs->dmabusmode.pri_ratio = ratio;
509 }
510 
511 /* dmain_en */
emac_ll_enable_all_intr(emac_dma_dev_t * dma_regs)512 static inline void emac_ll_enable_all_intr(emac_dma_dev_t *dma_regs)
513 {
514     dma_regs->dmain_en.val = 0xFFFFFFFF;
515 }
516 
emac_ll_disable_all_intr(emac_dma_dev_t * dma_regs)517 static inline void emac_ll_disable_all_intr(emac_dma_dev_t *dma_regs)
518 {
519     dma_regs->dmain_en.val = 0x00000000;
520 }
521 
emac_ll_enable_corresponding_intr(emac_dma_dev_t * dma_regs,uint32_t mask)522 static inline void emac_ll_enable_corresponding_intr(emac_dma_dev_t *dma_regs, uint32_t mask)
523 {
524     dma_regs->dmain_en.val |= mask;
525 }
526 
emac_ll_disable_corresponding_intr(emac_dma_dev_t * dma_regs,uint32_t mask)527 static inline void emac_ll_disable_corresponding_intr(emac_dma_dev_t *dma_regs, uint32_t mask)
528 {
529     dma_regs->dmain_en.val &= ~mask;
530 }
531 
emac_ll_get_intr_enable_status(emac_dma_dev_t * dma_regs)532 static inline uint32_t emac_ll_get_intr_enable_status(emac_dma_dev_t *dma_regs)
533 {
534     return dma_regs->dmain_en.val;
535 }
536 
537 /* dmastatus */
emac_ll_get_intr_status(emac_dma_dev_t * dma_regs)538 __attribute__((always_inline)) static inline uint32_t emac_ll_get_intr_status(emac_dma_dev_t *dma_regs)
539 {
540     return dma_regs->dmastatus.val;
541 }
542 
emac_ll_clear_corresponding_intr(emac_dma_dev_t * dma_regs,uint32_t bits)543 __attribute__((always_inline)) static inline void emac_ll_clear_corresponding_intr(emac_dma_dev_t *dma_regs, uint32_t bits)
544 {
545     dma_regs->dmastatus.val = bits;
546 }
547 
emac_ll_clear_all_pending_intr(emac_dma_dev_t * dma_regs)548 __attribute__((always_inline)) static inline void emac_ll_clear_all_pending_intr(emac_dma_dev_t *dma_regs)
549 {
550     dma_regs->dmastatus.val = 0xFFFFFFFF;
551 }
552 
553 
554 /* dmatxpolldemand / dmarxpolldemand */
emac_ll_transmit_poll_demand(emac_dma_dev_t * dma_regs,uint32_t val)555 static inline void emac_ll_transmit_poll_demand(emac_dma_dev_t *dma_regs, uint32_t val)
556 {
557     dma_regs->dmatxpolldemand = val;
558 }
emac_ll_receive_poll_demand(emac_dma_dev_t * dma_regs,uint32_t val)559 static inline void emac_ll_receive_poll_demand(emac_dma_dev_t *dma_regs, uint32_t val)
560 {
561     dma_regs->dmarxpolldemand = val;
562 }
563 
564 /*************** End of dma regs operation *********************/
565 
566 
567 
568 /************** Start of ext regs operation ********************/
emac_ll_clock_enable_mii(emac_ext_dev_t * ext_regs)569 static inline void emac_ll_clock_enable_mii(emac_ext_dev_t *ext_regs)
570 {
571     /* 0 for mii mode */
572     ext_regs->ex_phyinf_conf.phy_intf_sel = 0;
573     ext_regs->ex_clk_ctrl.mii_clk_rx_en = 1;
574     ext_regs->ex_clk_ctrl.mii_clk_tx_en = 1;
575 }
576 
emac_ll_clock_enable_rmii_input(emac_ext_dev_t * ext_regs)577 static inline void emac_ll_clock_enable_rmii_input(emac_ext_dev_t *ext_regs)
578 {
579     /* 4 for rmii mode */
580     ext_regs->ex_phyinf_conf.phy_intf_sel = 4;
581     /* ref clk for phy is input in rmii mode, the clk can be offered by mac layer or external crystal.
582     config pin as output to generate ref clk by esp32 mac layer or input to obtain the clock from external crystal */
583     ext_regs->ex_clk_ctrl.ext_en = 1;
584     ext_regs->ex_clk_ctrl.int_en = 0;
585     ext_regs->ex_oscclk_conf.clk_sel = 1;
586 }
587 
emac_ll_clock_enable_rmii_output(emac_ext_dev_t * ext_regs)588 static inline void emac_ll_clock_enable_rmii_output(emac_ext_dev_t *ext_regs)
589 {
590     /* 4 for rmii mode */
591     ext_regs->ex_phyinf_conf.phy_intf_sel = 4;
592     /* ref clk for phy is input in rmii mode, the clk can be offered by mac layer or external crystal.
593     config pin as output to generate ref clk by esp32 mac layer or input to obtain the clock from external crystal */
594     ext_regs->ex_clk_ctrl.ext_en = 0;
595     ext_regs->ex_clk_ctrl.int_en = 1;
596     ext_regs->ex_oscclk_conf.clk_sel = 0;
597     ext_regs->ex_clkout_conf.div_num = 0;
598     ext_regs->ex_clkout_conf.h_div_num = 0;
599 }
600 
601 
emac_ll_pause_frame_enable(emac_ext_dev_t * ext_regs,bool enable)602 static inline void emac_ll_pause_frame_enable(emac_ext_dev_t *ext_regs, bool enable)
603 {
604     ext_regs->ex_phyinf_conf.sbd_flowctrl = enable;
605 }
606 /*************** End of ext regs operation *********************/
607 
608 #ifdef __cplusplus
609 }
610 #endif
611