openeth.h 8.0 KB

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