fxmac_bd.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fxmac_bd.h
  15. * Date: 2022-04-06 14:46:52
  16. * LastEditTime: 2022-04-06 14:46:58
  17. * Description:  This file is for
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef DRIVERS_ETH_F_XMAC_BD_H
  24. #define DRIVERS_ETH_F_XMAC_BD_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include "ftypes.h"
  30. #include "string.h"
  31. /**
  32. * @name: FXMAC_BD_READ
  33. * @msg: Read the given Buffer Descriptor word.
  34. * @param bd_ptr is the base address of the BD to read
  35. * @param offset is the word offset to be read
  36. * @return The 32-bit value of the field
  37. */
  38. #define FXMAC_BD_READ(bd_ptr, offset) \
  39. (*(u32 *)((uintptr)((void *)(bd_ptr)) + (u32)(offset)))
  40. /**
  41. * @name: FXMAC_BD_WRITE
  42. * @msg: Write the given Buffer Descriptor word.
  43. * @param bd_ptr is the base address of the BD to write
  44. * @param Offset is the word offset to be written
  45. * @param data is the 32-bit value to write to the field
  46. * @return {*}
  47. */
  48. #define FXMAC_BD_WRITE(bd_ptr, Offset, data) \
  49. (*(u32 *)((uintptr)(void *)(bd_ptr) + (u32)(Offset)) = (u32)(data))
  50. /**
  51. * @name: FXMAC_BD_SET_STATUS
  52. * @msg: Set the BD's Status field (word 1).
  53. * @param bd_ptr is the BD pointer to operate on
  54. * @param data is the value to write to BD's status field.
  55. */
  56. #define FXMAC_BD_SET_STATUS(bd_ptr, data) \
  57. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  58. FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) | (data))
  59. /**
  60. * @name: FXMAC_BD_IS_RX_NEW
  61. * @msg: Determine the new bit of the receive BD.
  62. * @param bd_ptr is the BD pointer to operate on
  63. */
  64. #define FXMAC_BD_IS_RX_NEW(bd_ptr) \
  65. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_ADDR_OFFSET) & \
  66. FXMAC_RXBUF_NEW_MASK) != 0U \
  67. ? TRUE \
  68. : FALSE)
  69. /**
  70. * @name: FXMAC_BD_IS_TX_WRAP
  71. * @msg: Determine the wrap bit of the transmit BD which indicates end of the
  72. * BD list.
  73. * @param bd_ptr is the BD pointer to operate on
  74. */
  75. #define FXMAC_BD_IS_TX_WRAP(bd_ptr) \
  76. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  77. FXMAC_TXBUF_WRAP_MASK) != 0U \
  78. ? TRUE \
  79. : FALSE)
  80. /**
  81. * @name: FXMAC_BD_IS_RX_WRAP
  82. * @msg: Determine the wrap bit of the receive BD which indicates end of the
  83. * BD list.
  84. * @param: bd_ptr is the BD pointer to operate on
  85. */
  86. #define FXMAC_BD_IS_RX_WRAP(bd_ptr) \
  87. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_ADDR_OFFSET) & \
  88. FXMAC_RXBUF_WRAP_MASK) != 0U \
  89. ? TRUE \
  90. : FALSE)
  91. /**
  92. * @name: FXMAC_BD_SET_ADDRESS_TX
  93. * @msg: Set the BD's address field (word 0).
  94. * @param: bd_ptr is the BD pointer to operate on
  95. * @param: addr is the value to write to BD's status field.
  96. */
  97. #if defined(__aarch64__) || defined(__arch64__)
  98. #define FXMAC_BD_SET_ADDRESS_TX(bd_ptr, addr) \
  99. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_OFFSET, \
  100. (u32)((addr)&ULONG64_LO_MASK)); \
  101. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_HI_OFFSET, \
  102. (u32)(((addr)&ULONG64_HI_MASK) >> 32U));
  103. #else
  104. #define FXMAC_BD_SET_ADDRESS_TX(bd_ptr, addr) \
  105. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_OFFSET, (u32)(addr))
  106. #endif
  107. /**
  108. * @name: FXMAC_BD_SET_ADDRESS_RX
  109. * @msg: Set the BD's address field (word 0).
  110. * @param: bd_ptr is the BD pointer to operate on
  111. * @param: addr is the value to write to BD's status field.
  112. * @return {*}
  113. */
  114. #ifdef __aarch64__
  115. #define FXMAC_BD_SET_ADDRESS_RX(bd_ptr, addr) \
  116. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_OFFSET, \
  117. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_ADDR_OFFSET) & \
  118. ~FXMAC_RXBUF_ADD_MASK) | \
  119. ((u32)((addr)&ULONG64_LO_MASK)))); \
  120. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_HI_OFFSET, \
  121. (u32)(((addr)&ULONG64_HI_MASK) >> 32U));
  122. #else
  123. #define FXMAC_BD_SET_ADDRESS_RX(bd_ptr, addr) \
  124. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_ADDR_OFFSET, \
  125. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_ADDR_OFFSET) & \
  126. ~FXMAC_RXBUF_ADD_MASK) | \
  127. (u32)(addr)))
  128. #endif
  129. /**
  130. * @name: FXMAC_BD_SET_LENGTH
  131. * @msg: Set transfer length in bytes for the given BD. The length must be set each
  132. * time a BD is submitted to hardware.
  133. * @param: bd_ptr is the BD pointer to operate on
  134. * @param: len_bytes is the number of bytes to transfer.
  135. * @return {*}
  136. */
  137. #define FXMAC_BD_SET_LENGTH(bd_ptr, len_bytes) \
  138. FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  139. ((FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  140. ~FXMAC_TXBUF_LEN_MASK) | \
  141. (len_bytes)))
  142. /**
  143. * @name: FXMAC_BD_GET_LENGTH
  144. * @msg: For Tx channels, the returned value is the same as that written with
  145. * FXMAC_BD_SET_LENGTH(). For Rx channels, the returned value is the size of the received packet.
  146. * @param: bd_ptr is the BD pointer to operate on
  147. * @return {*}
  148. */
  149. #define FXMAC_BD_GET_LENGTH(bd_ptr) \
  150. (FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  151. FXMAC_RXBUF_LEN_MASK)
  152. /**
  153. * @name: FXMAC_GET_RX_FRAME_SIZE
  154. * @msg: The returned value is the size of the received packet.
  155. * This API supports jumbo frame sizes if enabled.
  156. * @param instance_p is the pointer to xmac instance
  157. * @param bd_ptr is the BD pointer to operate on
  158. *
  159. * @return Length field processed by hardware or set by
  160. * FXMAC_BD_SET_LENGTH().
  161. */
  162. #define FXMAC_BD_JUMBO_LENGTH_MASK
  163. #define FXMAC_GET_RX_FRAME_SIZE(instance_p, bd_ptr) \
  164. (FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  165. 0x00003FFFU)
  166. /**
  167. * @name: FXMAC_BD_CLEAR_TX_USED
  168. * @msg: Software clears this bit to enable the buffer to be read by the hardware.
  169. * Hardware sets this bit for the first buffer of a frame once it has been
  170. * successfully transmitted. This macro clears this bit of transmit BD.
  171. * @param: bd_ptr is the BD pointer to operate on
  172. * @return {*}
  173. */
  174. #define FXMAC_BD_CLEAR_TX_USED(bd_ptr) \
  175. (FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  176. FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  177. (~FXMAC_TXBUF_USED_MASK)))
  178. #define FXMAC_BD_SET_CRC(bd_ptr) \
  179. (FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  180. FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  181. (~FXMAC_TXBUF_NOCRC_MASK)))
  182. /**
  183. * @name: FXMAC_BD_SET_LAST
  184. * @msg: Tell the DMA engine that the given transmit BD marks the end of the current
  185. * packet to be processed.
  186. * @param bd_ptr is the BD pointer to operate on
  187. * @return {*}
  188. */
  189. #define FXMAC_BD_SET_LAST(bd_ptr) \
  190. (FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  191. FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) | \
  192. FXMAC_TXBUF_LAST_MASK))
  193. /**
  194. * @name: FXMAC_BD_CLEAR_LAST
  195. * @msg: Tell the DMA engine that the current packet does not end with the given
  196. * BD.
  197. * @param bd_ptr is the BD pointer to operate on
  198. * @return {*}
  199. */
  200. #define FXMAC_BD_CLEAR_LAST(bd_ptr) \
  201. (FXMAC_BD_WRITE((bd_ptr), FXMAC_BD_STAT_OFFSET, \
  202. FXMAC_BD_READ((bd_ptr), FXMAC_BD_STAT_OFFSET) & \
  203. ~FXMAC_TXBUF_LAST_MASK))
  204. /**
  205. * @name: FXMAC_BD_CLEAR
  206. * @msg: Zero out BD fields
  207. * @param bd_ptr is the BD pointer to operate on
  208. * @return {*}
  209. */
  210. #define FXMAC_BD_CLEAR(bd_ptr) \
  211. memset((bd_ptr), 0, sizeof(FXmacBd))
  212. /************************** Constant Definitions *****************************/
  213. /**************************** Type Definitions *******************************/
  214. #ifdef __aarch64__
  215. /* Minimum BD alignment */
  216. #define FXMAC_DMABD_MINIMUM_ALIGNMENT 64U
  217. #define FXMAC_BD_NUM_WORDS 4U
  218. #else
  219. /* Minimum BD alignment */
  220. #define FXMAC_DMABD_MINIMUM_ALIGNMENT 4U
  221. #define FXMAC_BD_NUM_WORDS 2U
  222. #endif
  223. /**
  224. * The FXMAC_Bd is the type for buffer descriptors (BDs).
  225. */
  226. typedef u32 FXmacBd[FXMAC_BD_NUM_WORDS];
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif // !