emac_hal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "esp_err.h"
  13. #include "hal/eth_types.h"
  14. #include "soc/emac_dma_struct.h"
  15. #include "soc/emac_mac_struct.h"
  16. #include "soc/emac_ext_struct.h"
  17. /**
  18. * @brief Ethernet DMA TX Descriptor
  19. *
  20. */
  21. typedef struct {
  22. volatile union {
  23. struct {
  24. uint32_t Deferred : 1; /*!< MAC defers before transmission */
  25. uint32_t UnderflowErr : 1; /*!< DMA encountered an empty transmit buffer */
  26. uint32_t ExcessiveDeferral : 1; /*!< Excessive deferral of over 24,288 bit times */
  27. uint32_t CollisionCount : 4; /*!< Number of collisions occurred before transmitted */
  28. uint32_t VLanFrame : 1; /*!< Transmitted frame is a VLAN-type frame */
  29. uint32_t ExcessiveCollision : 1; /*!< Transmission aborted after 16 successive collisions */
  30. uint32_t LateCollision : 1; /*!< Collision occurred after the collision window */
  31. uint32_t NoCarrier : 1; /*!< Carrier Sense signal from the PHY was not asserted */
  32. uint32_t LossCarrier : 1; /*!< Loss of carrier occurred during transmission */
  33. uint32_t PayloadChecksumErr : 1; /*!< Checksum error in TCP/UDP/ICMP datagram payload */
  34. uint32_t FrameFlushed : 1; /*!< DMA or MTL flushed the frame */
  35. uint32_t JabberTimeout : 1; /*!< MAC transmitter has experienced a jabber timeout */
  36. uint32_t ErrSummary : 1; /*!< Error Summary */
  37. uint32_t IPHeadErr : 1; /*!< IP Header Error */
  38. uint32_t TxTimestampStatus : 1; /*!< Timestamp captured for the transmit frame */
  39. uint32_t VLANInsertControl : 2; /*!< VLAN tagging or untagging before transmitting */
  40. uint32_t SecondAddressChained : 1; /*!< Second address in the descriptor is Next Descriptor address */
  41. uint32_t TransmitEndRing : 1; /*!< Descriptor list reached its final descriptor */
  42. uint32_t ChecksumInsertControl : 2; /*!< Control checksum calculation and insertion */
  43. uint32_t CRCReplacementControl : 1; /*!< Control CRC replace */
  44. uint32_t TransmitTimestampEnable : 1; /*!< Enable IEEE1588 harware timestamping */
  45. uint32_t DisablePad : 1; /*!< Control add padding when frame short than 64 bytes */
  46. uint32_t DisableCRC : 1; /*!< Control append CRC to the end of frame */
  47. uint32_t FirstSegment : 1; /*!< Buffer contains the first segment of a frame */
  48. uint32_t LastSegment : 1; /*!< Buffer contains the last segment of a frame */
  49. uint32_t InterruptOnComplete : 1; /*!< Interrupt after frame transmitted */
  50. uint32_t Own : 1; /*!< Owner of this descriptor: DMA controller or host */
  51. };
  52. uint32_t Value;
  53. } TDES0;
  54. union {
  55. struct {
  56. uint32_t TransmitBuffer1Size : 13; /*!< First data buffer byte size */
  57. uint32_t Reserved : 3; /*!< Reserved */
  58. uint32_t TransmitBuffer2Size : 13; /*!< Second data buffer byte size */
  59. uint32_t SAInsertControl : 3; /*!< Control MAC add or replace Source Address field */
  60. };
  61. uint32_t Value;
  62. } TDES1;
  63. uint32_t Buffer1Addr; /*!< Buffer1 address pointer */
  64. uint32_t Buffer2NextDescAddr; /*!< Buffer2 or next descriptor address pointer */
  65. uint32_t Reserved1; /*!< Reserved */
  66. uint32_t Reserved2; /*!< Reserved */
  67. uint32_t TimeStampLow; /*!< Transmit Frame Timestamp Low */
  68. uint32_t TimeStampHigh; /*!< Transmit Frame Timestamp High */
  69. } eth_dma_tx_descriptor_t;
  70. #define EMAC_DMATXDESC_CHECKSUM_BYPASS 0 /*!< Checksum engine bypass */
  71. #define EMAC_DMATXDESC_CHECKSUM_IPV4HEADER 1 /*!< IPv4 header checksum insertion */
  72. #define EMAC_DMATXDESC_CHECKSUM_TCPUDPICMPSEGMENT 2 /*!< TCP/UDP/ICMP Checksum Insertion calculated over segment only */
  73. #define EMAC_DMATXDESC_CHECKSUM_TCPUDPICMPFULL 3 /*!< TCP/UDP/ICMP Checksum Insertion fully calculated */
  74. _Static_assert(sizeof(eth_dma_tx_descriptor_t) == 32, "eth_dma_tx_descriptor_t should occupy 32 bytes in memory");
  75. /**
  76. * @brief Ethernet DMA RX Descriptor
  77. *
  78. */
  79. typedef struct {
  80. volatile union {
  81. struct {
  82. uint32_t ExtendStatusAvailable : 1; /*!< Extended statsu is available in RDES4 */
  83. uint32_t CRCErr : 1; /*!< CRC error occurred on frame */
  84. uint32_t DribbleBitErr : 1; /*!< frame contains non int multiple of 8 bits */
  85. uint32_t ReceiveErr : 1; /*!< Receive error */
  86. uint32_t ReceiveWatchdogTimeout : 1; /*!< Receive Watchdog timeout */
  87. uint32_t FrameType : 1; /*!< Ethernet type or IEEE802.3 */
  88. uint32_t LateCollision : 1; /*!< Late collision occurred during reception */
  89. uint32_t TSAvailIPChecksumErrGiantFrame : 1; /*!< Timestamp available or IP Checksum error or Giant frame */
  90. uint32_t LastDescriptor : 1; /*!< Last buffer of the frame */
  91. uint32_t FirstDescriptor : 1; /*!< First buffer of the frame */
  92. uint32_t VLANTag : 1; /*!< VLAN Tag: received frame is a VLAN frame */
  93. uint32_t OverflowErr : 1; /*!< Frame was damaged due to buffer overflow */
  94. uint32_t LengthErr : 1; /*!< Frame size not matching with length field */
  95. uint32_t SourceAddrFilterFail : 1; /*!< SA field of frame failed the SA filter */
  96. uint32_t DescriptorErr : 1; /*!< Frame truncated and DMA doesn't own next descriptor */
  97. uint32_t ErrSummary : 1; /*!< Error Summary, OR of all errors in RDES */
  98. uint32_t FrameLength : 14; /*!< Byte length of received frame */
  99. uint32_t DestinationAddrFilterFail : 1; /*!< Frame failed in the DA Filter in the MAC */
  100. uint32_t Own : 1; /*!< Owner of this descriptor: DMA controller or host */
  101. };
  102. uint32_t Value;
  103. } RDES0;
  104. union {
  105. struct {
  106. uint32_t ReceiveBuffer1Size : 13; /*!< First data buffer size in bytes */
  107. uint32_t Reserved1 : 1; /*!< Reserved */
  108. uint32_t SecondAddressChained : 1; /*!< Seconde address is the Next Descriptor address */
  109. uint32_t ReceiveEndOfRing : 1; /*!< Descriptor reached its final descriptor */
  110. uint32_t ReceiveBuffer2Size : 13; /*!< Second data buffer size in bytes */
  111. uint32_t Reserved : 2; /*!< Reserved */
  112. uint32_t DisableInterruptOnComplete : 1; /*!< Disable the assertion of interrupt to host */
  113. };
  114. uint32_t Value;
  115. } RDES1;
  116. uint32_t Buffer1Addr; /*!< Buffer1 address pointer */
  117. uint32_t Buffer2NextDescAddr; /*!< Buffer2 or next descriptor address pointer */
  118. volatile union {
  119. struct {
  120. uint32_t IPPayloadType : 3; /*!< Type of payload in the IP datagram */
  121. uint32_t IPHeadErr : 1; /*!< IP header error */
  122. uint32_t IPPayloadErr : 1; /*!< IP payload error */
  123. uint32_t IPChecksumBypass : 1; /*!< Checksum offload engine is bypassed */
  124. uint32_t IPv4PacketReceived : 1; /*!< Received packet is an IPv4 packet */
  125. uint32_t IPv6PacketReceived : 1; /*!< Received packet is an IPv6 packet */
  126. uint32_t MessageType : 4; /*!< PTP Message Type */
  127. uint32_t PTPFrameType : 1; /*!< PTP message is over Ethernet or IPv4/IPv6 */
  128. uint32_t PTPVersion : 1; /*!< Version of PTP protocol */
  129. uint32_t TimestampDropped : 1; /*!< Timestamp dropped because of overflow */
  130. uint32_t Reserved1 : 1; /*!< Reserved */
  131. uint32_t AVPacketReceived : 1; /*!< AV packet is received */
  132. uint32_t AVTaggedPacketReceived : 1; /*!< AV tagged packet is received */
  133. uint32_t VLANTagPrioVal : 3; /*!< VLAN tag's user value in the received packekt */
  134. uint32_t Reserved2 : 3; /*!< Reserved */
  135. uint32_t Layer3FilterMatch : 1; /*!< Received frame matches one of the enabled Layer3 IP */
  136. uint32_t Layer4FilterMatch : 1; /*!< Received frame matches one of the enabled Layer4 IP */
  137. uint32_t Layer3Layer4FilterNumberMatch : 2; /*!< Number of Layer3 and Layer4 Filter that matches the received frame */
  138. uint32_t Reserved3 : 4; /*!< Reserved */
  139. };
  140. uint32_t Value;
  141. } ExtendedStatus;
  142. uint32_t Reserved; /*!< Reserved */
  143. uint32_t TimeStampLow; /*!< Receive frame timestamp low */
  144. uint32_t TimeStampHigh; /*!< Receive frame timestamp high */
  145. } eth_dma_rx_descriptor_t;
  146. _Static_assert(sizeof(eth_dma_rx_descriptor_t) == 32, "eth_dma_rx_descriptor_t should occupy 32 bytes in memory");
  147. typedef struct {
  148. emac_mac_dev_t *mac_regs;
  149. emac_dma_dev_t *dma_regs;
  150. emac_ext_dev_t *ext_regs;
  151. uint8_t **rx_buf;
  152. uint8_t **tx_buf;
  153. void *descriptors;
  154. eth_dma_rx_descriptor_t *rx_desc;
  155. eth_dma_tx_descriptor_t *tx_desc;
  156. } emac_hal_context_t;
  157. /**
  158. * @brief EMAC related configuration
  159. */
  160. typedef struct {
  161. eth_mac_dma_burst_len_t dma_burst_len; /*!< eth-type enum of chosen dma burst-len */
  162. } emac_hal_dma_config_t;
  163. void emac_hal_init(emac_hal_context_t *hal, void *descriptors,
  164. uint8_t **rx_buf, uint8_t **tx_buf);
  165. void emac_hal_iomux_init_mii(void);
  166. void emac_hal_iomux_init_rmii(void);
  167. void emac_hal_iomux_rmii_clk_input(void);
  168. void emac_hal_iomux_rmii_clk_ouput(int num);
  169. void emac_hal_iomux_init_tx_er(void);
  170. void emac_hal_iomux_init_rx_er(void);
  171. void emac_hal_reset_desc_chain(emac_hal_context_t *hal);
  172. void emac_hal_reset(emac_hal_context_t *hal);
  173. bool emac_hal_is_reset_done(emac_hal_context_t *hal);
  174. void emac_hal_set_csr_clock_range(emac_hal_context_t *hal, int freq);
  175. void emac_hal_init_mac_default(emac_hal_context_t *hal);
  176. void emac_hal_init_dma_default(emac_hal_context_t *hal, emac_hal_dma_config_t *hal_config);
  177. void emac_hal_set_speed(emac_hal_context_t *hal, uint32_t speed);
  178. void emac_hal_set_duplex(emac_hal_context_t *hal, eth_duplex_t duplex);
  179. void emac_hal_set_promiscuous(emac_hal_context_t *hal, bool enable);
  180. /**
  181. * @brief Send MAC-CTRL frames to peer (EtherType=0x8808, opcode=0x0001, dest_addr=MAC-specific-ctrl-proto-01 (01:80:c2:00:00:01))
  182. */
  183. void emac_hal_send_pause_frame(emac_hal_context_t *hal, bool enable);
  184. bool emac_hal_is_mii_busy(emac_hal_context_t *hal);
  185. void emac_hal_set_phy_cmd(emac_hal_context_t *hal, uint32_t phy_addr, uint32_t phy_reg, bool write);
  186. void emac_hal_set_phy_data(emac_hal_context_t *hal, uint32_t reg_value);
  187. uint32_t emac_hal_get_phy_data(emac_hal_context_t *hal);
  188. void emac_hal_set_address(emac_hal_context_t *hal, uint8_t *mac_addr);
  189. /**
  190. * @brief Starts EMAC Transmission & Reception
  191. *
  192. * @param hal EMAC HAL context infostructure
  193. */
  194. void emac_hal_start(emac_hal_context_t *hal);
  195. /**
  196. * @brief Stops EMAC Transmission & Reception
  197. *
  198. * @param hal EMAC HAL context infostructure
  199. * @return
  200. * - ESP_OK: succeed
  201. * - ESP_ERR_INVALID_STATE: previous frame transmission/reception is not completed. When this error occurs,
  202. * wait and reapeat the EMAC stop again.
  203. */
  204. esp_err_t emac_hal_stop(emac_hal_context_t *hal);
  205. uint32_t emac_hal_get_tx_desc_owner(emac_hal_context_t *hal);
  206. uint32_t emac_hal_transmit_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t length);
  207. uint32_t emac_hal_transmit_multiple_buf_frame(emac_hal_context_t *hal, uint8_t **buffs, uint32_t *lengths, uint32_t inbuffs_cnt);
  208. uint32_t emac_hal_receive_frame(emac_hal_context_t *hal, uint8_t *buf, uint32_t size, uint32_t *frames_remain, uint32_t *free_desc);
  209. void emac_hal_enable_flow_ctrl(emac_hal_context_t *hal, bool enable);
  210. uint32_t emac_hal_get_intr_enable_status(emac_hal_context_t *hal);
  211. uint32_t emac_hal_get_intr_status(emac_hal_context_t *hal);
  212. void emac_hal_clear_corresponding_intr(emac_hal_context_t *hal, uint32_t bits);
  213. void emac_hal_clear_all_intr(emac_hal_context_t *hal);
  214. #ifdef __cplusplus
  215. }
  216. #endif