openeth.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stddef.h>
  9. #include <assert.h>
  10. #include "sdkconfig.h"
  11. #include "soc/soc.h"
  12. #include "esp_assert.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. // These are the register definitions for the OpenCores Ethernet MAC.
  17. // See comments in esp_eth_mac_openeth.c for more details about this driver.
  18. // DMA buffers configuration
  19. #define DMA_BUF_SIZE 1600
  20. #define RX_BUF_COUNT CONFIG_ETH_OPENETH_DMA_RX_BUFFER_NUM
  21. #define TX_BUF_COUNT CONFIG_ETH_OPENETH_DMA_TX_BUFFER_NUM
  22. // This driver uses the interrupt source number of the internal EMAC of the ESP32 chip,
  23. // and uses the same register address base. This of course only works in QEMU, where
  24. // the OpenCores MAC is mapped to the same register base and to the same interrupt
  25. // source. This driver does a sanity check that it is not running on the real ESP32
  26. // chip, using the EMAC date register.
  27. #define OPENETH_INTR_SOURCE ETS_ETH_MAC_INTR_SOURCE
  28. #define OPENETH_BASE DR_REG_EMAC_BASE
  29. // OpenCores ethmac registers
  30. #define OPENETH_MODER_REG (OPENETH_BASE + 0x00)
  31. #define OPENETH_MODER_DEFAULT 0xa000
  32. // OPENETH_RST: reset the MAC
  33. #define OPENETH_RST BIT(11)
  34. // OPENETH_PRO: enable promiscuous mode
  35. #define OPENETH_PRO BIT(5)
  36. // OPENETH_TXEN: enable transmit
  37. #define OPENETH_TXEN BIT(1)
  38. // OPENETH_RXEN: enable receive
  39. #define OPENETH_RXEN BIT(0)
  40. #define OPENETH_INT_SOURCE_REG (OPENETH_BASE + 0x04)
  41. #define OPENETH_INT_MASK_REG (OPENETH_BASE + 0x08)
  42. // These bits apply to INT_SOURCE and INT_MASK registers:
  43. // OPENETH_INT_BUSY: Buffer was received and discarded due to lack of buffers
  44. #define OPENETH_INT_BUSY BIT(4)
  45. // OPENETH_INT_RXB: Frame received
  46. #define OPENETH_INT_RXB BIT(2)
  47. // OPENETH_INT_TXB: Frame transmitted
  48. #define OPENETH_INT_TXB BIT(0)
  49. // IPGT, IPGR1, IPGR2 registers are not implemented in QEMU, hence not used here
  50. #define OPENETH_PACKETLEN_REG (OPENETH_BASE + 0x18)
  51. // OPENETH_MINFL: minimum frame length
  52. #define OPENETH_MINFL_S 16
  53. #define OPENETH_MINFL_V 0xffff
  54. #define OPENETH_MINFL_M (OPENETH_MINFL_V << OPENETH_MINFL_S)
  55. // OPENETH_MAXFL: maximum frame length
  56. #define OPENETH_MAXFL_S 0
  57. #define OPENETH_MAXFL_V 0xffff
  58. #define OPENETH_MAXFL_M (OPENETH_MAXFL_V << OPENETH_MAXFL_S)
  59. // COLLCONF is not implemented in QEMU
  60. #define OPENETH_TX_BD_NUM_REG (OPENETH_BASE + 0x20)
  61. // CTRLMODER, MIIMODER are not implemented in QEMU
  62. #define OPENETH_MIICOMMAND_REG (OPENETH_BASE + 0x2c)
  63. // OPENETH_WCTRLDATA: write control data
  64. #define OPENETH_WCTRLDATA BIT(2)
  65. // OPENETH_RSTAT: read status
  66. #define OPENETH_RSTAT BIT(1)
  67. // OPENETH_SCANSTAT: scan status
  68. #define OPENETH_SCANSTAT BIT(0)
  69. #define OPENETH_MIIADDRESS_REG (OPENETH_BASE + 0x30)
  70. // OPENETH_RGAD: register address
  71. #define OPENETH_RGAD_S 8
  72. #define OPENETH_RGAD_V 0x1f
  73. #define OPENETH_RGAD_M (OPENETH_RGAD_V << OPENETH_RGAD_S)
  74. // OPENETH_FIAD: PHY address
  75. #define OPENETH_FIAD_S 0
  76. #define OPENETH_FIAD_V 0x1f
  77. #define OPENETH_FIAD_N (OPENETH_FIAD_V << OPENETH_FIAD_S)
  78. #define OPENETH_MIITX_DATA_REG (OPENETH_BASE + 0x34)
  79. #define OPENETH_MIIRX_DATA_REG (OPENETH_BASE + 0x38)
  80. #define OPENETH_MII_DATA_MASK 0xffff
  81. #define OPENETH_MIISTATUS_REG (OPENETH_BASE + 0x3c)
  82. // OPENETH_LINKFAIL: link is down
  83. #define OPENETH_LINKFAIL BIT(0)
  84. // OPENETH_MAC_ADDR0_REG: bytes 2-5 of the MAC address (byte 5 in LSB)
  85. #define OPENETH_MAC_ADDR0_REG (OPENETH_BASE + 0x40)
  86. // OPENETH_MAC_ADDR1_REG: bytes 0-1 of the MAC address (byte 1 in LSB)
  87. #define OPENETH_MAC_ADDR1_REG (OPENETH_BASE + 0x44)
  88. #define OPENETH_HASH0_ADR_REG (OPENETH_BASE + 0x48)
  89. #define OPENETH_HASH1_ADR_REG (OPENETH_BASE + 0x4c)
  90. // Location of the DMA descriptors
  91. #define OPENETH_DESC_BASE (OPENETH_BASE + 0x400)
  92. // Total number of (TX + RX) DMA descriptors
  93. #define OPENETH_DESC_CNT 128
  94. // Structures describing TX and RX descriptors.
  95. // The field names are same as in the OpenCores ethmac documentation.
  96. typedef struct {
  97. uint16_t cs: 1; //!< Carrier sense lost (flag set by HW)
  98. uint16_t df: 1; //!< Defer indication (flag set by HW)
  99. uint16_t lc: 1; //!< Late collision occured (flag set by HW)
  100. uint16_t rl: 1; //!< TX failed due to retransmission limit (flag set by HW)
  101. uint16_t rtry: 4; //!< Number of retries before the frame was sent (set by HW)
  102. uint16_t ur: 1; //!< Underrun status (flag set by HW)
  103. uint16_t rsv: 2; //!< Reserved
  104. uint16_t crc: 1; //!< Add CRC at the end of the packet
  105. uint16_t pad: 1; //!< Add padding to the end of short packets
  106. uint16_t wr: 1; //!< Wrap-around. 0: not the last descriptor in the table, 1: last descriptor.
  107. uint16_t irq: 1; //!< Generate interrupt after this descriptor is transmitted
  108. uint16_t rd: 1; //!< Descriptor ready. 0: descriptor owned by SW, 1: descriptor owned by HW. Cleared by HW.
  109. uint16_t len; //!< Number of bytes to be transmitted
  110. void* txpnt; //!< Pointer to the data to transmit
  111. } openeth_tx_desc_t;
  112. ESP_STATIC_ASSERT(sizeof(openeth_tx_desc_t) == 8, "incorrect size of openeth_tx_desc_t");
  113. typedef struct {
  114. uint16_t lc: 1; //!< Late collision flag
  115. uint16_t crc: 1; //!< RX CRC error flag
  116. uint16_t sf: 1; //!< Frame shorter than set in PACKETLEN register
  117. uint16_t tl: 1; //!< Frame longer than set in PACKETLEN register
  118. uint16_t dn: 1; //!< Dribble nibble (frame length not divisible by 8 bits) flag
  119. uint16_t is: 1; //!< Invalid symbol flag
  120. uint16_t or: 1; //!< Overrun flag
  121. uint16_t m: 1; //!< Frame received because of the promiscuous mode
  122. uint16_t rsv: 5; //!< Reserved
  123. uint16_t wr: 1; //!< Wrap-around. 0: not the last descriptor in the table, 1: last descriptor.
  124. uint16_t irq: 1; //!< Generate interrupt after this descriptor is transmitted
  125. uint16_t e: 1; //!< The buffer is empty. 0: descriptor owned by SW, 1: descriptor owned by HW.
  126. uint16_t len; //!< Number of bytes received (filled by HW)
  127. void* rxpnt; //!< Pointer to the receive buffer
  128. } openeth_rx_desc_t;
  129. ESP_STATIC_ASSERT(sizeof(openeth_rx_desc_t) == 8, "incorrect size of openeth_rx_desc_t");
  130. static inline openeth_tx_desc_t* openeth_tx_desc(int idx)
  131. {
  132. assert(idx < TX_BUF_COUNT);
  133. return &((openeth_tx_desc_t*)OPENETH_DESC_BASE)[idx];
  134. }
  135. static inline openeth_rx_desc_t* openeth_rx_desc(int idx)
  136. {
  137. assert(idx < OPENETH_DESC_CNT - TX_BUF_COUNT);
  138. return &((openeth_rx_desc_t*)OPENETH_DESC_BASE)[idx + TX_BUF_COUNT];
  139. }
  140. static inline void openeth_enable(void)
  141. {
  142. REG_SET_BIT(OPENETH_MODER_REG, OPENETH_TXEN | OPENETH_RXEN | OPENETH_PRO);
  143. REG_SET_BIT(OPENETH_INT_MASK_REG, OPENETH_INT_RXB);
  144. }
  145. static inline void openeth_disable(void)
  146. {
  147. REG_CLR_BIT(OPENETH_INT_MASK_REG, OPENETH_INT_RXB);
  148. REG_CLR_BIT(OPENETH_MODER_REG, OPENETH_TXEN | OPENETH_RXEN | OPENETH_PRO);
  149. }
  150. static inline void openeth_reset(void)
  151. {
  152. REG_SET_BIT(OPENETH_MODER_REG, OPENETH_RST);
  153. REG_CLR_BIT(OPENETH_MODER_REG, OPENETH_RST);
  154. }
  155. static inline void openeth_init_tx_desc(openeth_tx_desc_t* desc, void* buf)
  156. {
  157. *desc = (openeth_tx_desc_t) {
  158. .rd = 0,
  159. .txpnt = buf
  160. };
  161. }
  162. static inline void openeth_init_rx_desc(openeth_rx_desc_t* desc, void* buf)
  163. {
  164. *desc = (openeth_rx_desc_t) {
  165. .e = 1,
  166. .irq = 1,
  167. .rxpnt = buf
  168. };
  169. }
  170. static inline void openeth_set_tx_desc_cnt(int tx_desc_cnt)
  171. {
  172. assert(tx_desc_cnt <= OPENETH_DESC_CNT);
  173. REG_WRITE(OPENETH_TX_BD_NUM_REG, tx_desc_cnt);
  174. }
  175. #ifdef __cplusplus
  176. }
  177. #endif