fxmac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.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_H
  24. #define DRIVERS_ETH_F_XMAC_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include "ftypes.h"
  30. #include "fassert.h"
  31. #include "ferror_code.h"
  32. #include "fxmac_hw.h"
  33. #include "fxmac_bdring.h"
  34. #include "fparameters.h"
  35. #define FXMAC_ERR_INVALID_PARAM FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x1u)
  36. #define FXMAC_ERR_SG_LIST FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x2u)
  37. #define FXMAC_ERR_GENERAL FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x4u)
  38. #define FXMAC_ERR_SG_NO_LIST FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x5u)
  39. #define FXMAC_ERR_PHY_BUSY FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x6u)
  40. #define FXMAC_PHY_IS_NOT_FOUND FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x7u)
  41. #define FXMAC_PHY_AUTO_AUTONEGOTIATION_FAILED FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x8u)
  42. #define FXMAC_ERR_MAC_IS_PROCESSING FT_MAKE_ERRCODE(ErrModBsp, ErrBspEth, 0x9u)
  43. /** @name Configuration options
  44. *
  45. * Device configuration options. See the FXMAC_SetOptions(),
  46. * FXMACClearOptions() and FXMAC_GetOptions() for information on how to
  47. * use options.
  48. *
  49. * The default state of the options are noted and are what the device and
  50. * driver will be set to after calling FXMAC_Reset() or
  51. * FXMAC_Initialize().
  52. *
  53. * @{
  54. */
  55. #define FXMAC_PROMISC_OPTION 0x00000001U
  56. /* Accept all incoming packets.
  57. * This option defaults to disabled (cleared) */
  58. #define FXMAC_FRAME1536_OPTION 0x00000002U
  59. /* Frame larger than 1516 support for Tx & Rx.x
  60. * This option defaults to disabled (cleared) */
  61. #define FXMAC_VLAN_OPTION 0x00000004U
  62. /* VLAN Rx & Tx frame support.
  63. * This option defaults to disabled (cleared) */
  64. #define FXMAC_FLOW_CONTROL_OPTION 0x00000010U
  65. /* Enable recognition of flow control frames on Rx
  66. * This option defaults to enabled (set) */
  67. #define FXMAC_FCS_STRIP_OPTION 0x00000020U
  68. /* Strip FCS and PAD from incoming frames. Note: PAD from VLAN frames is not
  69. * stripped.
  70. * This option defaults to enabled (set) */
  71. #define FXMAC_FCS_INSERT_OPTION 0x00000040U
  72. /* Generate FCS field and add PAD automatically for outgoing frames.
  73. * This option defaults to disabled (cleared) */
  74. #define FXMAC_LENTYPE_ERR_OPTION 0x00000080U
  75. /* Enable Length/Type error checking for incoming frames. When this option is
  76. * set, the MAC will filter frames that have a mismatched type/length field
  77. * and if FXMAC_REPORT_RXERR_OPTION is set, the user is notified when these
  78. * types of frames are encountered. When this option is cleared, the MAC will
  79. * allow these types of frames to be received.
  80. *
  81. * This option defaults to disabled (cleared) */
  82. #define FXMAC_TRANSMITTER_ENABLE_OPTION 0x00000100U
  83. /* Enable the transmitter.
  84. * This option defaults to enabled (set) */
  85. #define FXMAC_RECEIVER_ENABLE_OPTION 0x00000200U
  86. /* Enable the receiver
  87. * This option defaults to enabled (set) */
  88. #define FXMAC_BROADCAST_OPTION 0x00000400U
  89. /* Allow reception of the broadcast address
  90. * This option defaults to enabled (set) */
  91. #define FXMAC_MULTICAST_OPTION 0x00000800U
  92. /* Allows reception of multicast addresses programmed into hash
  93. * This option defaults to disabled (clear) */
  94. #define FXMAC_RX_CHKSUM_ENABLE_OPTION 0x00001000U
  95. /* Enable the RX checksum offload
  96. * This option defaults to enabled (set) */
  97. #define FXMAC_TX_CHKSUM_ENABLE_OPTION 0x00002000U
  98. /* Enable the TX checksum offload
  99. * This option defaults to enabled (set) */
  100. #define FXMAC_JUMBO_ENABLE_OPTION 0x00004000U
  101. #define FXMAC_SGMII_ENABLE_OPTION 0x00008000U
  102. #define FXMAC_LOOPBACK_NO_MII_OPTION 0x00010000U
  103. #define FXMAC_LOOPBACK_USXGMII_OPTION 0x00020000U
  104. #define FXMAC_GET_TXRING(instance) (instance.tx_bd_queue.bdring)
  105. #define FXMAC_GET_RXRING(instance) (instance.rx_bd_queue.bdring)
  106. #define FXMAC_DEFAULT_OPTIONS \
  107. ((u32)FXMAC_FLOW_CONTROL_OPTION | \
  108. (u32)FXMAC_FCS_INSERT_OPTION | \
  109. (u32)FXMAC_FCS_STRIP_OPTION | \
  110. (u32)FXMAC_BROADCAST_OPTION | \
  111. (u32)FXMAC_LENTYPE_ERR_OPTION | \
  112. (u32)FXMAC_TRANSMITTER_ENABLE_OPTION | \
  113. (u32)FXMAC_RECEIVER_ENABLE_OPTION | \
  114. (u32)FXMAC_RX_CHKSUM_ENABLE_OPTION | \
  115. (u32)FXMAC_TX_CHKSUM_ENABLE_OPTION)
  116. typedef enum
  117. {
  118. FXMAC_LINKDOWN = 0,
  119. FXMAC_LINKUP = 1,
  120. FXMAC_NEGOTIATING = 2
  121. } FXmacLinkStatus;
  122. /* The next few constants help upper layers determine the size of memory
  123. * pools used for Ethernet buffers and descriptor lists.
  124. */
  125. #define FXMAC_MAC_ADDR_SIZE 6U /* size of Ethernet header */
  126. #define FXMAC_MTU 1500U /* max MTU size of Ethernet frame */
  127. #define FXMAC_MTU_JUMBO 10240U /* max MTU size of jumbo frame */
  128. #define FXMAC_HDR_SIZE 14U /* size of Ethernet header , DA + SA + TYPE*/
  129. #define FXMAC_HDR_VLAN_SIZE 18U /* size of Ethernet header with VLAN */
  130. #define FXMAC_TRL_SIZE 4U /* size of Ethernet trailer (FCS) */
  131. #define FXMAC_MAX_FRAME_SIZE (FXMAC_MTU + FXMAC_HDR_SIZE + \
  132. FXMAC_TRL_SIZE)
  133. #define FXMAC_MAX_VLAN_FRAME_SIZE (FXMAC_MTU + FXMAC_HDR_SIZE + \
  134. FXMAC_HDR_VLAN_SIZE + FXMAC_TRL_SIZE)
  135. #define FXMAC_MAX_VLAN_FRAME_SIZE_JUMBO (FXMAC_MTU_JUMBO + FXMAC_HDR_SIZE + \
  136. FXMAC_HDR_VLAN_SIZE + FXMAC_TRL_SIZE)
  137. #define FXMAC_MAX_FRAME_SIZE_JUMBO (FXMAC_MTU_JUMBO + FXMAC_HDR_SIZE + FXMAC_TRL_SIZE)
  138. /** @name Callback identifiers
  139. *
  140. * These constants are used as parameters to FXMAC_SetHandler()
  141. * @{
  142. */
  143. #define FXMAC_HANDLER_DMASEND 1U /* 发送中断 */
  144. #define FXMAC_HANDLER_DMARECV 2U /* 接收中断 */
  145. #define FXMAC_HANDLER_ERROR 3U /* 异常中断 */
  146. #define FXMAC_HANDLER_LINKCHANGE 4U /* 连接状态 */
  147. #define FXMAC_HANDLER_RESTART 5U /* 发送描述符队列发生异常 */
  148. /*@}*/
  149. #define FXMAC_DMA_SG_IS_STARTED 0
  150. #define FXMAC_DMA_SG_IS_STOPED 1
  151. #define FXMAC_SPEED_10 10U
  152. #define FXMAC_SPEED_100 100U
  153. #define FXMAC_SPEED_1000 1000U
  154. #define FXMAC_SPEED_2500 2500U
  155. #define FXMAC_SPEED_5000 5000U
  156. #define FXMAC_SPEED_10000 10000U
  157. #define FXMAC_SPEED_25000 25000U
  158. /** @name Direction identifiers
  159. *
  160. * These are used by several functions and callbacks that need
  161. * to specify whether an operation specifies a send or receive channel.
  162. * @{
  163. */
  164. #define FXMAC_SEND 1U /* send direction */
  165. #define FXMAC_RECV 2U /* receive direction */
  166. /****************************************************************************/
  167. /**
  168. *
  169. * This macro triggers trasmit circuit to send data currently in TX buffer(s).
  170. *
  171. * @param instance_p is a pointer to the FXmac instance to be worked on.
  172. *
  173. * @return
  174. *
  175. * @note
  176. *
  177. * Signature: void FXmacTransmit(FXmac *instance_p)
  178. *
  179. *****************************************************************************/
  180. #define FXmacTransmit(instance_p) \
  181. FXMAC_WRITEREG32((instance_p)->config.base_address, \
  182. FXMAC_NWCTRL_OFFSET, \
  183. (FXMAC_READREG32((instance_p)->config.base_address, \
  184. FXMAC_NWCTRL_OFFSET) | \
  185. FXMAC_NWCTRL_STARTTX_MASK))
  186. typedef void (*FXmacIrqHandler)(void *args);
  187. typedef void (*FXmacErrorIrqHandler)(void *args, u32 direction, u32 error_word);
  188. /* Interface Mode definitions */
  189. typedef enum
  190. {
  191. FXMAC_PHY_INTERFACE_MODE_SGMII,
  192. FXMAC_PHY_INTERFACE_MODE_RMII,
  193. FXMAC_PHY_INTERFACE_MODE_RGMII,
  194. FXMAC_PHY_INTERFACE_MODE_XGMII,
  195. FXMAC_PHY_INTERFACE_MODE_USXGMII,
  196. } FXmacPhyInterface;
  197. typedef struct
  198. {
  199. u32 instance_id; /* Id of device*/
  200. volatile uintptr_t base_address;
  201. volatile uintptr_t extral_mode_base;
  202. volatile uintptr_t extral_loopback_base;
  203. FXmacPhyInterface interface;
  204. u32 speed; /* FXMAC_SPEED_XXX */
  205. u32 duplex; /* 1 is full-duplex , 0 is half-duplex */
  206. u32 auto_neg; /* Enable auto-negotiation - when set active high, autonegotiation operation is enabled. */
  207. u32 pclk_hz;
  208. u32 max_queue_num; /* Number of Xmac Controller Queues */
  209. u32 tx_queue_id; /* 0 ~ FT_XMAC_QUEUE_MAX_NUM ,Index queue number */
  210. u32 rx_queue_id; /* 0 ~ FT_XMAC_QUEUE_MAX_NUM ,Index queue number */
  211. u32 hotplug_irq_num;
  212. u32 dma_brust_length; /* burst length */
  213. u32 network_default_config;
  214. u32 queue_irq_num[FT_XMAC_QUEUE_MAX_NUM]; /* mac0 8个 ,其他的 4个 */
  215. } FXmacConfig;
  216. typedef struct
  217. {
  218. u32 queue_id;
  219. FXmacBdRing bdring;
  220. } FXmacQueue;
  221. typedef struct
  222. {
  223. FXmacConfig config;
  224. u32 is_ready; /* Device is ininitialized and ready*/
  225. u32 is_started;
  226. u32 link_status; /* indicates link status ,FXMAC_LINKUP is link up ,FXMAC_LINKDOWN is link down,FXMAC_NEGOTIATING is need to negotiating*/
  227. u32 options;
  228. FXmacQueue tx_bd_queue; /* Transmit Queue */
  229. FXmacQueue rx_bd_queue; /* Receive Queue */
  230. FXmacIrqHandler send_irq_handler;
  231. void *send_args;
  232. FXmacIrqHandler recv_irq_handler;
  233. void *recv_args;
  234. FXmacErrorIrqHandler error_irq_handler;
  235. void *error_args;
  236. FXmacIrqHandler link_change_handler;
  237. void *link_change_args;
  238. FXmacIrqHandler restart_handler;
  239. void *restart_args;
  240. u32 moudle_id; /* Module identification number */
  241. u32 max_mtu_size;
  242. u32 max_frame_size;
  243. u32 phy_address; /* phy address */
  244. u32 rxbuf_mask; /* 1000,100,10 */
  245. } FXmac;
  246. /* fxmac_sinit.c */
  247. const FXmacConfig *FXmacLookupConfig(u32 instance_id);
  248. /* fgmac.c */
  249. FError FXmacCfgInitialize(FXmac *instance_p, const FXmacConfig *config_p);
  250. void FXmacInitInterface(FXmac *instance_p);
  251. void FXmacGetMacAddress(FXmac *instance_p, u8 *address_ptr, u8 index);
  252. FError FXmacSetMacAddress(FXmac *instance_p, u8 *address_ptr, u8 index);
  253. FError FXmacSetOptions(FXmac *instance_p, u32 options, u32 queue_num);
  254. FError FXmacClearOptions(FXmac *instance_p, u32 options, u32 queue_num);
  255. void FXmacStart(FXmac *instance_p);
  256. void FXmacStop(FXmac *instance_p);
  257. void FXmacSetQueuePtr(FXmac *instance_p, uintptr QPtr, u8 QueueNum,
  258. u32 direction);
  259. /* phy interface */
  260. FError FXmacPhyWrite(FXmac *instance_p, u32 phy_address,
  261. u32 register_num, u16 phy_data);
  262. FError FXmacPhyRead(FXmac *instance_p, u32 phy_address,
  263. u32 register_num, u16 *phydat_aptr);
  264. FError FXmacPhyInit(FXmac *instance_p, u32 speed, u32 duplex_mode, u32 autonegotiation_en);
  265. void FXmacSelectClk(FXmac *instance_p);
  266. FError FXmacSetHandler(FXmac *instance_p, u32 handler_type, void *func_pointer, void *call_back_ref);
  267. /* interrupt */
  268. void FXmacIntrHandler(s32 vector, void *args);
  269. void FXmacClearHash(FXmac *instance_p);
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif // !