hpm_tsw_drv.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2023-2024 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_TSW_DRV_H
  8. #define HPM_TSW_DRV_H
  9. /*---------------------------------------------------------------------
  10. * Includes
  11. *--------------------------------------------------------------------*/
  12. #include "hpm_common.h"
  13. #include "hpm_soc_feature.h"
  14. #include "hpm_tsw_regs.h"
  15. /**
  16. * @brief TSW driver APIs
  17. * @defgroup tsw_interface TSW driver APIs
  18. * @ingroup communication_interfaces
  19. * @{
  20. */
  21. /*---------------------------------------------------------------------
  22. * Macro Constant Declarations
  23. *-------------------------------------------------------------------*/
  24. #define MAC_LO(mac) (uint32_t)(mac[0] | (mac[1] << 8) | (mac[2] << 16) | (mac[3] << 24))
  25. #define MAC_HI(mac) (uint32_t)(mac[4] | (mac[5] << 8))
  26. #define MAC_MDIO_CTRL_OP_WR (0x01)
  27. #define MAC_MDIO_CTRL_OP_RD (0x02)
  28. /*---------------------------------------------------------------------
  29. * Typedef Struct Declarations
  30. *-------------------------------------------------------------------*/
  31. typedef struct {
  32. union {
  33. uint32_t tx_hdr0;
  34. struct {
  35. uint32_t dest_port: 8; /**< dest port */
  36. uint32_t : 8; /**< reserved */
  37. uint32_t queue : 3; /**< the priority queue for TSW TX */
  38. uint32_t utag : 3; /**< TSW-EP TX user sideband information */
  39. uint32_t : 6; /**< reserved */
  40. uint32_t htype : 4; /**< header type */
  41. } tx_hdr0_bm;
  42. };
  43. union {
  44. uint32_t tx_hdr1;
  45. struct {
  46. uint32_t cb: 32; /**< CB field. Optionally used for external stream identification */
  47. } tx_hdr1_bm;
  48. };
  49. uint32_t tx_hdr2; /**< reserved */
  50. uint32_t tx_hdr3; /**< reserved */
  51. } tx_hdr_desc_t;
  52. /*---------------------------------------------------------------------
  53. * Typedef Enum Declarations
  54. *-------------------------------------------------------------------*/
  55. typedef enum {
  56. tsw_port_speed_10mbps = 2,
  57. tsw_port_speed_100mbps = 3,
  58. tsw_port_speed_1000mbps = 0
  59. } tsw_port_speed_t;
  60. typedef enum {
  61. tsw_port_phy_itf_mii = 0,
  62. tsw_port_phy_itf_rmii = 4,
  63. tsw_port_phy_itf_rgmii = 1
  64. } tsw_port_phy_itf_t;
  65. typedef enum {
  66. tsw_dst_port_null = 0,
  67. tsw_dst_port_cpu = 1 << 0,
  68. tsw_dst_port_1 = 1 << 1,
  69. tsw_dst_port_2 = 1 << 2,
  70. tsw_dst_port_3 = 1 << 3,
  71. } tsw_dst_t;
  72. typedef enum {
  73. tsw_mac_mode_mii = 0,
  74. tsw_mac_mode_gmii
  75. } tsw_mac_mode_t;
  76. #if defined __cplusplus
  77. extern "C" {
  78. #endif /* __cplusplus */
  79. /*---------------------------------------------------------------------
  80. * Exported Functions
  81. *-------------------------------------------------------------------*/
  82. /**
  83. * @brief Send a frame from CPU port
  84. *
  85. * @param[in] ptr TSW peripheral base address
  86. * @param[in] length Frame length
  87. * @param[in] id Frame index
  88. * @return Result of the transmission
  89. */
  90. hpm_stat_t tsw_send(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
  91. /**
  92. * @brief Setup a receive description
  93. *
  94. * @param[in] ptr TSW peripheral base address
  95. * @param[in] buffer Pointer to the specified receive buffer
  96. * @param[in] length Frame length
  97. * @param[in] id Frame index
  98. * @return Result of the setup of a receive description
  99. */
  100. hpm_stat_t tsw_recv_setup(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
  101. /**
  102. * @brief Receive a frame
  103. *
  104. * @param[in] ptr TSW peripheral base address
  105. * @param[in] buffer Pointer to the specified receive buffer
  106. * @param[in] length Buffer length
  107. * @param[in] id Frame index
  108. * @return Result of the received frame
  109. */
  110. uint32_t tsw_recv(TSW_Type *ptr, uint32_t *buffer, uint32_t length, uint8_t id);
  111. /**
  112. * @brief Lookup Bypass Setting
  113. *
  114. * @param[in] ptr TSW peripheral base address
  115. * @param[in] dst_port Destination port number
  116. */
  117. void tsw_mac_lookup_bypass(TSW_Type *ptr, uint8_t dst_port);
  118. /**
  119. * @brief CAM VLAN Setting
  120. *
  121. * @param[in] ptr TSW peripheral base address
  122. * @param[in] dst_port Destination port number
  123. */
  124. void tsw_set_cam_vlan_port(TSW_Type *ptr);
  125. /**
  126. * @brief MDIO Interface Config
  127. *
  128. * @param[in] ptr TSW peripheral base address
  129. * @param[in] port TSW port number
  130. * @param[in] clk_div TSW clock division
  131. * @return Result of MDIO interface config
  132. */
  133. hpm_stat_t tsw_ep_set_mdio_config(TSW_Type *ptr, uint8_t port, uint8_t clk_div);
  134. /**
  135. * @brief MDIO Read
  136. *
  137. * @param[in] ptr TSW peripheral base address
  138. * @param[in] port TSW port number
  139. * @param[in] phy_addr TSW clock division
  140. * @param[in] reg_addr PHY register address
  141. * @param[out] data Pointer to data memory
  142. * @return Result of MDIO read
  143. */
  144. hpm_stat_t tsw_ep_mdio_read(TSW_Type *ptr, uint8_t port, uint32_t phy_addr, uint32_t reg_addr, uint16_t *data);
  145. /**
  146. * @brief MDIO Write
  147. *
  148. * @param[in] ptr TSW peripheral base address
  149. * @param[in] port TSW port number
  150. * @param[in] phy_addr TSW clock division
  151. * @param[in] reg_addr PHY register address
  152. * @param[in] data Data value
  153. * @return Result of MDIO write
  154. */
  155. hpm_stat_t tsw_ep_mdio_write(TSW_Type *ptr, uint8_t port, uint32_t phy_addr, uint32_t reg_addr, uint16_t data);
  156. /**
  157. * @brief Enable MAC Controller
  158. *
  159. * @param[in] ptr TSW peripheral base address
  160. * @param[in] port TSW port number
  161. * @param[in] mac_type MAC type 0:EMAC/1:PMAC
  162. * @param[in] enable Enable MAC Controller: Set true to enable; Set false to disable
  163. * @return Result of controlling MAC controller
  164. */
  165. hpm_stat_t tsw_ep_enable_mac_ctrl(TSW_Type *ptr, uint8_t port, uint8_t mac_type, bool enable);
  166. /**
  167. * @brief Set MAC Address
  168. *
  169. * @param[in] ptr TSW peripheral base address
  170. * @param[in] port TSW port number
  171. * @param[in] mac_addr Pointer to MAC address
  172. * @param[in] promisc Promiscuous Mode: Set true to enable; set false to disable
  173. * @return Result of setting MAC address
  174. */
  175. hpm_stat_t tsw_ep_set_mac_addr(TSW_Type *ptr, uint8_t port, uint8_t *mac_addr, bool promisc);
  176. /**
  177. * @brief Set MAC Mode
  178. *
  179. * @param[in] ptr TSW peripheral base address
  180. * @param[in] port TSW port number
  181. * @param[in] mac_addr Pointer to MAC address
  182. * @param[in] promisc Promiscuous Mode: Set true to enable; set false to disable
  183. * @return Result of setting MAC address
  184. */
  185. hpm_stat_t tsw_ep_set_mac_mode(TSW_Type *ptr, uint8_t port, uint8_t gmii);
  186. /**
  187. * @brief Set Port GPR
  188. *
  189. * @param[in] ptr TSW peripheral base address
  190. * @param[in] port TSW port number
  191. * @param[in] speed Pointer to MAC address
  192. * @param[in] itf Promiscuous Mode: Set true to enable; set false to disable
  193. * @param[in] tx_dly Tx delay
  194. * @param[in] rx_dlay Rx delay
  195. */
  196. void tsw_port_gpr(TSW_Type *ptr, uint8_t port, uint8_t speed, uint8_t itf, uint8_t tx_dly, uint8_t rx_dly);
  197. /**
  198. * @brief Set Internal Frame Action
  199. *
  200. * @param[in] ptr TSW peripheral base address
  201. * @param[in] dest_port Destination port number
  202. */
  203. void tsw_set_internal_frame_action(TSW_Type *ptr, uint8_t dest_port);
  204. /**
  205. * @brief Set Broadcast Frame Action
  206. *
  207. * @param[in] ptr TSW peripheral base address
  208. * @param[in] dest_port Destination port number
  209. */
  210. void tsw_set_broadcast_frame_action(TSW_Type *ptr, uint8_t dest_port);
  211. /**
  212. * @brief Set Unknow Frame Action
  213. *
  214. * @param[in] ptr TSW peripheral base address
  215. * @param[in] dest_port Destination port number
  216. */
  217. void tsw_set_unknown_frame_action(TSW_Type *ptr, uint8_t dest_port);
  218. /**
  219. * @brief Set Lookup Table
  220. *
  221. * @param[in] ptr TSW peripheral base address
  222. * @param[in] entry_num Entry number
  223. * @param[in] dest_port Destination port number
  224. * @param[in] dest_mac Destination MAC address
  225. */
  226. void tsw_set_lookup_table(TSW_Type *ptr, uint16_t entry_num, uint8_t dest_port, uint64_t dest_mac);
  227. /**
  228. * @brief Clear CAM
  229. *
  230. * @param[in] ptr TSW peripheral base address
  231. */
  232. void tsw_clear_cam(TSW_Type *ptr);
  233. /**
  234. * @brief Enable RXFIFO to store and forward mode
  235. *
  236. * @param[in] ptr TSW peripheral base address
  237. * @param[in] port TSW port number
  238. */
  239. void tsw_enable_store_forward_mode(TSW_Type *ptr, uint8_t port);
  240. /**
  241. * @brief Disable RXFIFO to store and forward mode
  242. *
  243. * @param[in] ptr TSW peripheral base address
  244. * @param[in] port TSW port number
  245. */
  246. void tsw_disable_store_forward_mode(TSW_Type *ptr, uint8_t port);
  247. #if defined __cplusplus
  248. }
  249. #endif /* __cplusplus */
  250. /** @} */
  251. #endif /* HPM_TSW_DRV_H */