hpm_enet_drv.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_ENET_DRV_H
  8. #define HPM_ENET_DRV_H
  9. /*---------------------------------------------------------------------
  10. * Includes
  11. *---------------------------------------------------------------------
  12. */
  13. #include "hpm_common.h"
  14. #include "hpm_enet_regs.h"
  15. #include "hpm_soc_feature.h"
  16. #include "hpm_enet_soc_drv.h"
  17. /**
  18. * @brief Enet driver APIs
  19. * @defgroup enet_interface Enet driver APIs
  20. * @ingroup communication_interfaces
  21. * @{
  22. */
  23. /*---------------------------------------------------------------------
  24. * Macro Constant Declarations
  25. *---------------------------------------------------------------------
  26. */
  27. #define ENET_HEADER (14U) /**< 6-byte Dest addr, 6-byte Src addr, 2-byte type */
  28. #define ENET_EXTRA (2U) /**< Extra bytes in some cases */
  29. #define ENET_VLAN_TAG (4U) /**< optional 802.1q VLAN Tag */
  30. #define ENET_CRC (4U) /**< Ethernet CRC */
  31. #define ENET_MIN_PAYLOAD (46U) /**< Minimum Ethernet payload size */
  32. #define ENET_MAX_PAYLOAD (1500U) /**< Maximum Ethernet payload size */
  33. #define ENET_MAX_FRAME_SIZE (1524U) /**< ENET_HEADER + ENET_EXTRA + VLAN_TAG + MAX_ENET_PAYLOAD + ENET_CRC */
  34. #define ENET_JUMBO_FRAME_PAYLOAD (9000U) /**< Jumbo frame payload size */
  35. #define ENET_MAC (6) /**< Ethernet MAC size */
  36. #define ENET_ERROR (0) /**< ENET error */
  37. #define ENET_SUCCESS (1) /**< ENET success */
  38. #define ENET_ADJ_FREQ_BASE_ADDEND (0x80000000UL) /**< PTP base adjustment addend */
  39. #define ENET_ONE_SEC_IN_NANOSEC (1000000000UL) /**< one second in nanoseconds */
  40. #define ENET_PPS_CMD_MASK (0x07UL) /**< Enet PPS CMD Mask */
  41. #define ENET_PPS_CMD_OFS_FAC (3U) /**< Enet PPS CMD OFS Factor */
  42. #ifndef ENET_RETRY_CNT
  43. #define ENET_RETRY_CNT (10000UL) /**< Enet retry count for PTP */
  44. #endif
  45. /*---------------------------------------------------------------------
  46. * Typedef Enum Declarations
  47. *---------------------------------------------------------------------
  48. */
  49. /** @brief Programmable burst length selections */
  50. typedef enum {
  51. enet_normal_int_sum_en = ENET_DMA_INTR_EN_NIE_MASK,
  52. enet_aboarmal_int_sum_en = ENET_DMA_INTR_EN_AIE_MASK,
  53. enet_receive_int_en = ENET_DMA_INTR_EN_RIE_MASK
  54. } enet_interrupt_enable_t;
  55. /** @brief Programmable burst length selections */
  56. typedef enum {
  57. enet_lpi_int_mask = ENET_INTR_MASK_LPIIM_MASK,
  58. enet_rgsmii_int_mask = ENET_INTR_MASK_RGSMIIIM_MASK
  59. } enet_interrupt_mask_t;
  60. /** @brief Programmable burst length selections */
  61. typedef enum {
  62. enet_pbl_1 = 1,
  63. enet_pbl_2 = 2,
  64. enet_pbl_4 = 4,
  65. enet_pbl_8 = 8,
  66. enet_pbl_16 = 16,
  67. enet_pbl_32 = 32
  68. } enet_pbl_t;
  69. /** @brief Checksum insertion control selections */
  70. typedef enum {
  71. enet_cic_disable = 0,
  72. enet_cic_ip = 1,
  73. enet_cic_ip_no_pseudoheader = 2,
  74. enet_cic_ip_pseudoheader = 3
  75. } enet_cic_insertion_control_t;
  76. /** @brief VLAN insertion control selections */
  77. typedef enum {
  78. enet_vlic_disable = 0,
  79. enet_vlic_remove_vlan_tag = 1,
  80. enet_vlic_insert_vlan_tag = 2,
  81. enet_vlic_replace_vlan_tag = 3
  82. } enet_vlan_insertion_control_t;
  83. /** @brief SA insertion or replacement control selections for any selective frames */
  84. typedef enum {
  85. enet_saic_disable = 0,
  86. enet_saic_insert_mac0 = 1,
  87. enet_saic_replace_mac0 = 2,
  88. enet_saic_insert_mac1 = 5,
  89. enet_saic_replace_mac1 = 6
  90. } enet_saic_insertion_replacement_control_t;
  91. /** @brief SA insertion or replacement control selections for all transmit frames */
  92. typedef enum {
  93. enet_sarc_disable = 0,
  94. enet_sarc_insert_mac0 = 2,
  95. enet_sarc_replace_mac0 = 3,
  96. enet_sarc_insert_mac1 = 6,
  97. enet_sarc_replace_mac1 = 7
  98. } enet_sarc_insertion_replacement_control_t;
  99. /** @brief PHY operation selections */
  100. typedef enum {
  101. enet_phy_op_read = 0,
  102. enet_phy_op_write
  103. } enet_phy_op_t;
  104. /** @brief PHY status */
  105. typedef enum {
  106. enet_gmii_idle = 0,
  107. enet_gmii_busy
  108. } enet_gmii_status_t;
  109. /** @brief CSR clock range and MDC clock selections */
  110. /** @note The suggested range of CSR clock is approximately
  111. * between the frequency range 1.0MHz-2.5MHz.
  112. * You can achieve higher frequency of the MDC clock than the frequency limit of 2.5MHz(specified in the IEEE Std 802.3)
  113. * and program a clock divider of lower value. Program the value which is no less than enet_csr_60m_to_100m_mdc_csr_div_4
  114. * only if the interfacing chips support faster MDC clocks.
  115. */
  116. typedef enum {
  117. enet_csr_60m_to_100m_mdc_csr_div_42 = 0, /**< CSR clock range: 60-100MHz <==> MDC clock: CSR clock / 42 */
  118. enet_csr_100m_to_150m_mdc_csr_div_62, /**< CSR clock range: 100-150MHz <==> MDC clock: CSR clock / 62 */
  119. enet_csr_20m_to_35m_mdc_csr_div_16, /**< CSR clock range: 20-35MHz <==> MDC clock: CSR clock / 16 */
  120. enet_csr_35m_to_60m_mdc_csr_div_26, /**< CSR clock range: 35-60MHz <==> MDC clock: CSR clock / 26 */
  121. enet_csr_150m_to_250m_mdc_csr_div_102, /**< CSR clock range: 150-250MHz <==> MDC clock: CSR clock / 102 */
  122. enet_csr_250m_to_300m_mdc_csr_div_124, /**< CSR clock range: 250-300MHz <==> MDC clock: CSR clock / 124 */
  123. enet_csr_60m_to_100m_mdc_csr_div_4 = 8, /**< CSR clock / 4 */
  124. enet_csr_60m_to_100m_mdc_csr_div_6, /**< CSR clock / 6 */
  125. enet_csr_60m_to_100m_mdc_csr_div_8, /**< CSR clock / 8 */
  126. enet_csr_60m_to_100m_mdc_csr_div_10, /**< CSR clock / 10 */
  127. enet_csr_60m_to_100m_mdc_csr_div_12, /**< CSR clock / 12 */
  128. enet_csr_60m_to_100m_mdc_csr_div_14, /**< CSR clock / 14 */
  129. enet_csr_60m_to_100m_mdc_csr_div_16, /**< CSR clock / 16 */
  130. enet_csr_60m_to_100m_mdc_csr_div_18 /**< CSR clock / 18 */
  131. } enet_csr_clk_range_t;
  132. /** @brief enet interface selections */
  133. typedef enum {
  134. enet_inf_rmii = 4,
  135. enet_inf_rgmii = 1
  136. } enet_inf_type_t;
  137. /** @brief enet line speed */
  138. typedef enum {
  139. enet_line_speed_1000mbps = 0,
  140. enet_line_speed_10mbps = 2,
  141. enet_line_speed_100mbps = 3
  142. } enet_line_speed_t;
  143. /** @brief enet duplex mode */
  144. typedef enum {
  145. enet_half_duplex = 0,
  146. enet_full_duplex
  147. } enet_duplex_mode_t;
  148. /** @brief enet timestamp update methods */
  149. typedef enum {
  150. enet_ptp_time_coarse_update = 0,
  151. enet_ptp_time_fine_update
  152. } enet_ptp_time_update_method_t;
  153. /** @brief PTP versions */
  154. typedef enum {
  155. enet_ptp_v1 = 0,
  156. enet_ptp_v2
  157. } enet_ptp_version_t;
  158. /** @brief PTP frame types */
  159. typedef enum {
  160. enet_ptp_frame_ipv4 = 0,
  161. enet_ptp_frame_ipv6,
  162. enet_ptp_frame_ethernet
  163. } enet_ptp_frame_type_t;
  164. /** @brief PTP message type for snapshots */
  165. typedef enum {
  166. enet_ts_ss_ptp_msg_0 = 0, /* SYNC, Follow_Up, Delay_Req, Delay_Resp */
  167. enet_ts_ss_ptp_msg_1 = 1, /* SYNC */
  168. enet_ts_ss_ptp_msg_2 = 3, /* Delay_Req */
  169. enet_ts_ss_ptp_msg_3 = 4, /* SYNC, Follow_Up, Delay_Req, Delay_Resp, Pdelay_Req, Pdelay_Resp, Pdelay_Resp_Follow_Up */
  170. enet_ts_ss_ptp_msg_4 = 5, /* SYNC, Pdelay_Req, Pdelay_Resp */
  171. enet_ts_ss_ptp_msg_5 = 7, /* Delay_Req, Pdelay_Req, Pdelay_Resp */
  172. enet_ts_ss_ptp_msg_6 = 8, /* SYNC, Delay_Req */
  173. enet_ts_ss_ptp_msg_7 = 12 /* Pdelay_Req, Pdelay_Resp */
  174. } enet_ts_ss_ptp_msg_t;
  175. /** @brief PTP timer rollover modes */
  176. typedef enum {
  177. enet_ts_bin_rollover_control = 0, /* timestamp rolls over after 0x7fffffff */
  178. enet_ts_dig_rollover_control /* timestamp rolls over after 0x3b9ac9ff */
  179. } enet_ts_rollover_control_t;
  180. /** @brief PPS indexes */
  181. typedef enum {
  182. enet_pps_0 = -1,
  183. enet_pps_1 = 0,
  184. enet_pps_2 = 1,
  185. enet_pps_3 = 2
  186. } enet_pps_idx_t;
  187. /** @brief PPS0 control for output frequency selections */
  188. typedef enum {
  189. enet_pps_ctrl_pps = 0,
  190. enet_pps_ctrl_bin_2hz_digital_1hz,
  191. enet_pps_ctrl_bin_4hz_digital_2hz,
  192. enet_pps_ctrl_bin_8hz_digital_4hz,
  193. enet_pps_ctrl_bin_16hz_digital_8hz,
  194. enet_pps_ctrl_bin_32hz_digital_16hz,
  195. enet_pps_ctrl_bin_64hz_digital_32hz,
  196. enet_pps_ctrl_bin_128hz_digital_64hz,
  197. enet_pps_ctrl_bin_256hz_digital_128hz,
  198. enet_pps_ctrl_bin_512hz_digital_256hz,
  199. enet_pps_ctrl_bin_1024hz_digital_512hz,
  200. enet_pps_ctrl_bin_2048hz_digital_1024hz,
  201. enet_pps_ctrl_bin_4096hz_digital_2048hz,
  202. enet_pps_ctrl_bin_8192hz_digital_4096hz,
  203. enet_pps_ctrl_bin_16384hz_digital_8192hz,
  204. enet_pps_ctrl_bin_32867hz_digital_16384hz
  205. } enet_pps_ctrl_t;
  206. /** @brief PPS0 commands */
  207. typedef enum {
  208. enet_pps_cmd_no_command = 0,
  209. enet_pps_cmd_start_single_pulse,
  210. enet_pps_cmd_start_pulse_train,
  211. enet_pps_cmd_cancel_start,
  212. enet_pps_cmd_stop_pulse_train_at_time,
  213. enet_pps_cmd_stop_pulse_train_immediately,
  214. enet_pps_cmd_cancel_stop_pulse_train
  215. } enet_pps_cmd_t;
  216. /*---------------------------------------------------------------------
  217. * Typedef Struct Declarations
  218. *---------------------------------------------------------------------
  219. */
  220. /** @brief enet buffer config struct */
  221. typedef struct {
  222. uint32_t buffer;
  223. uint32_t count;
  224. uint16_t size;
  225. } enet_buff_config_t;
  226. /** @brief enet mac config struct */
  227. typedef struct {
  228. uint32_t mac_addr_high[ENET_SOC_ADDR_MAX_COUNT];
  229. uint32_t mac_addr_low[ENET_SOC_ADDR_MAX_COUNT];
  230. uint8_t valid_max_count;
  231. uint8_t dma_pbl;
  232. uint8_t sarc;
  233. } enet_mac_config_t;
  234. /** @brief transmission descriptor struct */
  235. typedef struct {
  236. union {
  237. uint32_t tdes0;
  238. struct {
  239. uint32_t db: 1; /**< * Deferred Bit*/
  240. uint32_t uf: 1; /**< * Underflow Error */
  241. uint32_t ed: 1; /**< * Excessive Deferral */
  242. uint32_t cc: 4; /**< * Collision Count */
  243. uint32_t vf: 1; /**< * VLAN Frame */
  244. uint32_t ec: 1; /**< * Excessive Collision */
  245. uint32_t lc: 1; /**< * Late Collision */
  246. uint32_t nc: 1; /**< * No Carrier */
  247. uint32_t loc: 1; /**< * Loss of Carrier */
  248. uint32_t ipe: 1; /**< * IP Payload Error */
  249. uint32_t ff: 1; /**< * Frame Flushed */
  250. uint32_t jt: 1; /**< * Jabber Timeout */
  251. uint32_t es: 1; /**< * Error Summary */
  252. uint32_t ihe: 1; /**< * IP Header Error */
  253. uint32_t ttss: 1; /**< * Transmit Timestamp Status */
  254. uint32_t vlic: 2; /**< * VLAN Insertion Control */
  255. uint32_t tch: 1; /**< * Second Address Chained */
  256. uint32_t ter: 1; /**< * Transmit End of Ring */
  257. uint32_t cic: 2; /**< * Checksum Insertion Control */
  258. uint32_t crcr: 1; /**< * CRC Replacement Control */
  259. uint32_t ttse: 1; /**< * Transmit Timestamp Enable */
  260. uint32_t dp: 1; /**< * Disable Pad */
  261. uint32_t dc: 1; /**< * Disable CRC */
  262. uint32_t fs: 1; /**< * First Segment */
  263. uint32_t ls: 1; /**< * Last Segment */
  264. uint32_t ic: 1; /**< * Interrupt on Completion */
  265. uint32_t own: 1; /**< * Own Bit */
  266. } tdes0_bm;
  267. };
  268. union {
  269. uint32_t tdes1;
  270. struct {
  271. uint32_t tbs1 : 13; /**< Transmit Buffer 1 Size */
  272. uint32_t reserved: 3; /**< Reserved */
  273. uint32_t tbs2 : 13; /**< Transmit Buffer 2 Size */
  274. uint32_t saic : 3; /**< SA Insertion Control */
  275. } tdes1_bm;
  276. };
  277. union {
  278. uint32_t tdes2;
  279. struct {
  280. uint32_t buffer1; /**< Buffer 1 Address */
  281. } tdes2_bm;
  282. };
  283. union {
  284. uint32_t tdes3;
  285. union {
  286. uint32_t buffer2; /**< Buffer 2 Address */
  287. uint32_t next_desc; /**< Next Descriptor Address */
  288. } tdes3_bm;
  289. };
  290. #if ENET_SOC_ALT_EHD_DES_LEN == ENET_SOC_ALT_EHD_DES_MAX_LEN
  291. struct {
  292. uint32_t reserved;
  293. } tdes4_bm;
  294. struct {
  295. uint32_t reserved;
  296. } tdes5_bm;
  297. struct {
  298. uint32_t ttsl; /**< Transmit Frame Timestamp Low */
  299. } tdes6_bm;
  300. struct {
  301. uint32_t ttsh; /**< Transmit Frame Timestamp High */
  302. } tdes7_bm;
  303. #endif
  304. } enet_tx_desc_t;
  305. /** @brief reception descriptor struct */
  306. typedef struct {
  307. union {
  308. uint32_t rdes0;
  309. struct {
  310. uint32_t ex_sta_rx_addr : 1; /**< Extended Status Available or Rx MAC Address*/
  311. uint32_t ce : 1; /**< CRC Error */
  312. uint32_t dbe : 1; /**< Dribble Bit Error */
  313. uint32_t re : 1; /**< Receive Error */
  314. uint32_t rwt : 1; /**< Receive Watchdog Timeout */
  315. uint32_t ft : 1; /**< Frame Type */
  316. uint32_t lc : 1; /**< Late Collision */
  317. uint32_t ts_ip_gf : 1; /**< Timestamp Available, IP Checksum Error or Giant Frame*/
  318. uint32_t ls : 1; /**< Last Descriptor */
  319. uint32_t fs : 1; /**< First Descriptor */
  320. uint32_t vlan : 1; /**< VLAN Tag */
  321. uint32_t oe : 1; /**< Overflow Error */
  322. uint32_t le : 1; /**< Length Error */
  323. uint32_t saf : 1; /**< Source Address Filter Fail */
  324. uint32_t dse : 1; /**< Descriptor Error */
  325. uint32_t es : 1; /**< Error Summary */
  326. uint32_t fl : 14; /**< Frame Length */
  327. uint32_t afm : 1; /**< Destination Address Filter Fail */
  328. uint32_t own : 1; /**< Own Bit */
  329. } rdes0_bm;
  330. };
  331. union {
  332. uint32_t rdes1;
  333. struct {
  334. uint32_t rbs1 : 13; /**< Receive Buffer 1 Size */
  335. uint32_t reserved0: 1; /**< Reserved */
  336. uint32_t rch : 1; /**< Second Address Chained */
  337. uint32_t rer : 1; /**< Receive End of Ring */
  338. uint32_t rbs2 : 13; /**< Receive Buffer 2 Size */
  339. uint32_t reserved1: 2; /**< Reserved */
  340. uint32_t dic : 1; /**< Disable Interrupt on Completion */
  341. } rdes1_bm;
  342. };
  343. union {
  344. uint32_t rdes2;
  345. struct {
  346. uint32_t buffer1; /**< Buffer 1 Address */
  347. } rdes2_bm;
  348. };
  349. union {
  350. uint32_t rdes3;
  351. union {
  352. uint32_t buffer2; /**< Buffer 2 Address */
  353. uint32_t next_desc; /**< Next Descriptor Address */
  354. } rdes3_bm;
  355. };
  356. #if ENET_SOC_ALT_EHD_DES_LEN == ENET_SOC_ALT_EHD_DES_MAX_LEN
  357. union {
  358. uint32_t rdes4;
  359. struct {
  360. uint32_t ip_payload_type : 3; /**< IP Payload Type */
  361. uint32_t ip_header_err : 1; /**< IP Header Error */
  362. uint32_t ip_payload_err : 1; /**< IP Payload Error */
  363. uint32_t ip_chksum_bypassed : 1; /**< IP Checksum Bypassed */
  364. uint32_t ipv4_pkt_received : 1; /**< IPv4 Packet Received */
  365. uint32_t ipv6_pkt_received : 1; /**< IPv6 Packet Received */
  366. uint32_t msg_type : 4; /**< Message Type */
  367. uint32_t ptp_frame_type : 1; /**< PTP Frame Type */
  368. uint32_t ptp_version : 1; /**< PTP Version */
  369. uint32_t ts_dp : 1; /**< Timestamp Dropped */
  370. uint32_t reserved0 : 1; /**< Reserved */
  371. uint32_t av_pkt_recv : 1; /**< AV Packet Received */
  372. uint32_t av_tagged_pkt_recv : 1; /**< AV Tagged Packet Received */
  373. uint32_t vlan_tag_pri_value : 3; /**< VLAN Tag Priority Value */
  374. uint32_t reserved1 : 3; /**< Reserved */
  375. uint32_t l3_fm : 1; /**< Layer 3 Filter Matched */
  376. uint32_t l4_fm : 1; /**< Layer 4 Filter Matched */
  377. uint32_t l3_l4_fnl : 2; /**< Layer 3 and Layer 4 Filter Number Matched */
  378. uint32_t reserved2 : 4; /**< Reserved */
  379. } rdes4_bm;
  380. };
  381. struct {
  382. uint32_t reserved;
  383. } rdes5_bm;
  384. struct {
  385. uint32_t rtsl; /**< Receive Frame Timestamp Low */
  386. } rdes6_bm;
  387. struct {
  388. uint32_t rtsh; /**< Receive Frame Timestamp High */
  389. } rdes7_bm;
  390. #endif
  391. } enet_rx_desc_t;
  392. /** @brief enet frame struct */
  393. typedef struct{
  394. uint32_t length;
  395. uint32_t buffer;
  396. enet_rx_desc_t *rx_desc;
  397. } enet_frame_t;
  398. /** @brief enet reception frame info struct */
  399. typedef struct {
  400. enet_rx_desc_t *fs_rx_desc;
  401. enet_rx_desc_t *ls_rx_desc;
  402. uint32_t seg_count;
  403. } enet_rx_frame_info_t;
  404. /** @brief enet control config struct for transmission */
  405. typedef struct {
  406. bool enable_ioc; /* interrupt on completion */
  407. bool disable_crc; /* disable CRC */
  408. bool disable_pad; /* disable Pad */
  409. bool enable_ttse; /* enable transmit timestamp */
  410. bool enable_crcr; /* CRC replacement control */
  411. uint8_t cic; /* checksum insertion control */
  412. uint8_t vlic; /* VLAN insertion control */
  413. uint8_t saic; /* SA insertion control */
  414. } enet_tx_control_config_t;
  415. /** @brief enet description struct */
  416. typedef struct {
  417. enet_tx_desc_t *tx_desc_list_head;
  418. enet_rx_desc_t *rx_desc_list_head;
  419. enet_tx_desc_t *tx_desc_list_cur;
  420. enet_rx_desc_t *rx_desc_list_cur;
  421. enet_buff_config_t tx_buff_cfg;
  422. enet_buff_config_t rx_buff_cfg;
  423. enet_rx_frame_info_t rx_frame_info;
  424. enet_tx_control_config_t tx_control_config;
  425. } enet_desc_t;
  426. /** @brief PTP system timestamp struct */
  427. typedef struct {
  428. uint32_t sec;
  429. uint32_t nsec;
  430. } enet_ptp_ts_system_t;
  431. /** @brief PTP update timestamp struct */
  432. typedef struct {
  433. uint32_t sec;
  434. uint32_t nsec;
  435. uint8_t sign;
  436. } enet_ptp_ts_update_t;
  437. /** @brief PTP target timestamp struct */
  438. typedef struct {
  439. uint32_t sec;
  440. uint32_t nsec;
  441. } enet_ptp_ts_target_t;
  442. /** @brief PTP config strcut */
  443. typedef struct {
  444. uint8_t ssinc;
  445. uint8_t timestamp_rollover_mode;
  446. uint8_t update_method;
  447. uint32_t addend;
  448. } enet_ptp_config_t;
  449. /** @brief PTP PPS command output config strcut */
  450. typedef struct {
  451. uint32_t pps_interval;
  452. uint32_t pps_width;
  453. uint32_t target_sec;
  454. uint32_t target_nsec;
  455. } enet_pps_cmd_config_t;
  456. /** @brief Enet interrupt config struct */
  457. typedef struct {
  458. uint32_t int_enable; /* DMA_INTR_EN */
  459. uint32_t int_mask; /* INTR MASK */
  460. uint32_t mmc_intr_rx;
  461. uint32_t mmc_intr_mask_rx;
  462. uint32_t mmc_intr_tx;
  463. uint32_t mmc_intr_mask_tx;
  464. } enet_int_config_t;
  465. /*
  466. * @brief Bit definition of TDES1
  467. */
  468. #define ENET_DMATxDesc_TBS2 ((uint32_t)0x1FFF0000) /**< Transmit Buffer2 Size */
  469. #define ENET_DMATxDesc_TBS1 ((uint32_t)0x00001FFF) /**< Transmit Buffer1 Size */
  470. #if defined __cplusplus
  471. extern "C" {
  472. #endif /* __cplusplus */
  473. /*---------------------------------------------------------------------
  474. * Exported Functions
  475. *---------------------------------------------------------------------
  476. */
  477. /**
  478. * @brief Get a default control config for tranmission
  479. *
  480. * @param[in] ptr An Ethernet peripheral base address
  481. * @param[in] config A pointer to a control config structure for tranmission
  482. */
  483. void enet_get_default_tx_control_config(ENET_Type *ptr, enet_tx_control_config_t *config);
  484. /**
  485. * @brief Get interrupt status
  486. *
  487. * @param[in] ptr An Ethernet peripheral base address
  488. * @return A result of interrupt status
  489. */
  490. uint32_t enet_get_interrupt_status(ENET_Type *ptr);
  491. /**
  492. * @brief Mask the specified mmc interrupt evenets of received frames
  493. *
  494. * @param[in] ptr An Ethernet peripheral base address
  495. * @param[in] config A mask of the specified evenets
  496. */
  497. void enet_mask_mmc_rx_interrupt_event(ENET_Type *ptr, uint32_t mask);
  498. /**
  499. * @brief Mask the specified mmc interrupt evenets of transmitted frames
  500. *
  501. * @param[in] ptr An Ethernet peripheral base address
  502. * @param[in] config A mask of the specified evenets
  503. */
  504. void enet_mask_mmc_tx_interrupt_event(ENET_Type *ptr, uint32_t mask);
  505. /**
  506. * @brief Get a staus of mmc receive interrupt events
  507. *
  508. * @param[in] ptr An Ethernet peripheral base address
  509. * @return A result of interrupt status
  510. */
  511. uint32_t enet_get_mmc_rx_interrupt_status(ENET_Type *ptr);
  512. /**
  513. * @brief et a staus of mmc transmission interrupt events
  514. *
  515. * @param[in] ptr An Ethernet peripheral base address
  516. * @return A result of interrupt status
  517. */
  518. uint32_t enet_get_mmc_tx_interrupt_status(ENET_Type *ptr);
  519. /**
  520. * @brief Initialize controller
  521. *
  522. * @param[in] ptr An Ethernet peripheral base address
  523. * @param[in] inf_type the specified interface
  524. * @param[in] desc A pointer to descriptor config
  525. * @param[in] cfg A pointer to mac config
  526. * @param[in] int_cfg A pointer to the masks of the specified enabled interrupts and the specified masked interrupts
  527. */
  528. int enet_controller_init(ENET_Type *ptr, enet_inf_type_t inf_type, enet_desc_t *desc, enet_mac_config_t *cfg, enet_int_config_t *int_config);
  529. /**
  530. * @brief Set port line speed
  531. *
  532. * @param[in] ptr An Ethernet peripheral base address
  533. * @param[in] line_speed An enum variable of @ref enet_line_speed_t
  534. */
  535. void enet_set_line_speed(ENET_Type *ptr, enet_line_speed_t speed);
  536. /**
  537. * @brief Set duplex mode
  538. *
  539. * @param[in] ptr An Ethernet peripheral base address
  540. * @param[in] mode An enum variable of @ref enet_duplex_mode_t
  541. */
  542. void enet_set_duplex_mode(ENET_Type *ptr, enet_duplex_mode_t mode);
  543. /**
  544. * @brief Read phy
  545. *
  546. * @param[in] ptr An Ethernet peripheral base address
  547. * @param[in] phy_addr the specified address of phy
  548. * @param[in] addr the specified address of register
  549. * @retval A value corresponding to the specified register address
  550. */
  551. uint16_t enet_read_phy(ENET_Type *ptr, uint32_t phy_addr, uint32_t addr);
  552. /**
  553. * @brief Write phy
  554. *
  555. * @param[in] ptr An Ethernet peripheral base address
  556. * @param[in] phy_addr a specified address of phy
  557. * @param[in] addr a specified address of the register
  558. * @param[in] data a specified data to be written
  559. */
  560. void enet_write_phy(ENET_Type *ptr, uint32_t phy_addr, uint32_t addr, uint32_t data);
  561. /**
  562. * @brief Check if there is a received frame
  563. *
  564. * @param[out] parent_rx_desc_list_cur a parent pointer to the current reception description list
  565. * @param[in] rx_frame_info A pointer to the information of the reception frames
  566. * @retval A result of reception frame.
  567. * 1 means that a reception of frame is successful.
  568. * 0 means that a reception of frame is unsuccessful.
  569. */
  570. uint32_t enet_check_received_frame(enet_rx_desc_t **parent_rx_desc_list_cur, enet_rx_frame_info_t *rx_frame_info);
  571. /**
  572. * @brief get a received frame
  573. *
  574. * @param[out] parent_rx_desc_list_cur A parent pointer to the current reception description list
  575. * @param[in] rx_frame_info A pointer to the information of the reception frames
  576. * @retval A struct of the current reception frame
  577. */
  578. enet_frame_t enet_get_received_frame(enet_rx_desc_t **parent_rx_desc_list_cur, enet_rx_frame_info_t *rx_frame_info);
  579. /**
  580. * @brief get a received frame from interrupt
  581. *
  582. * @param[out] parent_rx_desc_list_cur the parent pointer to the current reception description list
  583. * @param[in] rx_frame_info A pointer to the information of the reception frames
  584. * @param[in] rx_desc_count A total count of the reception descriptors
  585. * @retval A struct of the current reception frame
  586. */
  587. enet_frame_t enet_get_received_frame_interrupt(enet_rx_desc_t **parent_rx_desc_list_cur, enet_rx_frame_info_t *rx_frame_info, uint32_t rx_desc_count);
  588. /**
  589. * @brief prepare for the transmission descriptors (It will be deprecated.)
  590. *
  591. * @param[in] ptr An Ethernet peripheral base address
  592. * @param[out] parent_tx_desc_list_cur a pointer to the information of the reception frames
  593. * @param[in] frame_length the length of the transmission
  594. * @param[in] tx_buff_size the size of the transmission buffer
  595. * @retval a result of the transmission preparation.
  596. * 1 means that the preparation is successful.
  597. * 0 means that the preparation is unsuccessful.
  598. */
  599. uint32_t enet_prepare_transmission_descriptors(ENET_Type *ptr, enet_tx_desc_t **parent_tx_desc_list_cur, uint16_t frame_length, uint16_t tx_buff_size);
  600. /**
  601. * @brief prepare for the transmission descriptors
  602. *
  603. * @param[in] ptr An Ethernet peripheral base address
  604. * @param[out] parent_tx_desc_list_cur a pointer to the information of the reception frames
  605. * @param[in] config a pointer to the control configuration for the transmission frames
  606. * @param[in] frame_length the length of the transmission
  607. * @param[in] tx_buff_size the size of the transmission buffer
  608. * @retval a result of the transmission preparation.
  609. * 1 means that the preparation is successful.
  610. * 0 means that the preparation is unsuccessful.
  611. */
  612. uint32_t enet_prepare_tx_desc(ENET_Type *ptr, enet_tx_desc_t **parent_tx_desc_list_cur, enet_tx_control_config_t *config, uint16_t frame_length, uint16_t tx_buff_size);
  613. /**
  614. * @brief prepare for the transmission descriptors with a timestamp record
  615. *
  616. * @param[in] ptr An Ethernet peripheral base address
  617. * @param[out] parent_tx_desc_list_cur a pointer to the information of the reception frames
  618. * @param[in] config a pointer to the control configuration for the transmission frames
  619. * @param[in] frame_length the length of the transmission
  620. * @param[in] tx_buff_size the size of the transmission buffer
  621. * @param[out] timestamp a pointer to the timestamp record of a transmitted frame
  622. * @retval a result of the transmission preparation.
  623. * 1 means that the preparation is successful.
  624. * 0 means that the preparation is unsuccessful.
  625. */
  626. uint32_t enet_prepare_tx_desc_with_ts_record(ENET_Type *ptr,
  627. enet_tx_desc_t **parent_tx_desc_list_cur,
  628. enet_tx_control_config_t *config,
  629. uint16_t frame_length, uint16_t tx_buff_size,
  630. enet_ptp_ts_system_t *timestamp);
  631. /**
  632. * @brief Initialize DMA transmission descriptors in chain mode
  633. *
  634. * @param[in] ptr An Ethernet peripheral base address
  635. * @param[in] desc A pointer to transmission descriptors
  636. */
  637. void enet_dma_tx_desc_chain_init(ENET_Type *ptr, enet_desc_t *desc);
  638. /**
  639. * @brief Initialize DMA reception descriptors in chain mode
  640. *
  641. * @param[in] ptr An Ethernet peripheral base address
  642. * @param[in] desc A pointer to reception descriptors
  643. */
  644. void enet_dma_rx_desc_chain_init(ENET_Type *ptr, enet_desc_t *desc);
  645. /**
  646. * @brief Flush DMA
  647. *
  648. * @param[in] ptr An Ethernet peripheral base address
  649. */
  650. void enet_dma_flush(ENET_Type *ptr);
  651. /**
  652. * @brief Initialize a PTP timer
  653. *
  654. * @param[in] ptr An Ethernet peripheral base address
  655. * @param[in] config A pointer to an enet_ptp_config struct instance
  656. */
  657. void enet_init_ptp(ENET_Type *ptr, enet_ptp_config_t *config);
  658. /**
  659. * @brief Set a timestamp to the PTP timer
  660. *
  661. * @param[in] ptr An Ethernet peripheral base address
  662. * @param[in] timestamp A pointer to a update timestamp structure instance
  663. */
  664. void enet_set_ptp_timestamp(ENET_Type *ptr, enet_ptp_ts_update_t *timestamp);
  665. /**
  666. * @brief Get a timestamp from the PTP timer
  667. *
  668. * @param[in] ptr An Ethernet peripheral base address
  669. * @param[out] timestamp A pointer to a system timestamp structure instance
  670. */
  671. void enet_get_ptp_timestamp(ENET_Type *ptr, enet_ptp_ts_system_t *timestamp);
  672. /**
  673. * @brief Update a timestamp to the PTP timer
  674. *
  675. * @param[in] ptr An Ethernet peripheral base address
  676. * @param[in] timeoffset A pointer to a update timestamp structure instance
  677. */
  678. void enet_update_ptp_timeoffset(ENET_Type *ptr, enet_ptp_ts_update_t *timeoffset);
  679. /**
  680. * @brief Adjust the count frequency of the PTP timer
  681. *
  682. * @param[in] ptr An Ethernet peripheral base address
  683. * @param[in] adj An adjustment value for the count frequency of the PTP timer
  684. */
  685. void enet_adjust_ptp_time_freq(ENET_Type *ptr, int32_t adj);
  686. /**
  687. * @brief Set the PTP version
  688. *
  689. * @param[in] ptr An Ethernet peripheral base address
  690. * @param[in] ptp_ver An enum value indicating the PTP protocol
  691. */
  692. void enet_set_ptp_version(ENET_Type *ptr, enet_ptp_version_t ptp_ver);
  693. /**
  694. * @brief Enable the specified ptp frame type for MAC process
  695. *
  696. * @param[in] ptr An Ethernet peripheral base address
  697. * @param[in] ptp_frame_type An enum value indicating the transport protocol of PTP frames
  698. * @param[in] enable A value to enable or disable the transport protocol of PTP frames which is specified by ptp_frame_type parameter
  699. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success
  700. */
  701. hpm_stat_t enet_enable_ptp_frame_type(ENET_Type *ptr, enet_ptp_frame_type_t ptp_frame_type, bool enable);
  702. /**
  703. * @brief Set the ptp message type for snapshots
  704. *
  705. * @param[in] ptr An Ethernet peripheral base address
  706. * @param[in] ts_ss_ptp_msg An enum value indicating the specified ptp message type for snapshots
  707. */
  708. void enet_set_snapshot_ptp_message_type(ENET_Type *ptr, enet_ts_ss_ptp_msg_t ts_ss_ptp_msg);
  709. /**
  710. * @brief Set the pps0 control output
  711. *
  712. * @param[in] ptr An Ethernet peripheral base address
  713. * @param[in] enet_pps_ctrl_t An enum value indicating the specified pps frequency
  714. */
  715. void enet_set_pps0_control_output(ENET_Type *ptr, enet_pps_ctrl_t freq);
  716. /**
  717. * @brief Set a pps command for ppsx
  718. *
  719. * @param[in] ptr An Ethernet peripheral base address
  720. * @param[in] cmd An enum value indicating the specified pps command
  721. * @param[in] idx An enum value indicating the index of pps instance
  722. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success
  723. */
  724. hpm_stat_t enet_set_ppsx_command(ENET_Type *ptr, enet_pps_cmd_t cmd, enet_pps_idx_t idx);
  725. /**
  726. * @brief Set a pps config for ppsx
  727. *
  728. * @param[in] ptr An Ethernet peripheral base address
  729. * @param[in] cmd An enum value indicating the specified pps config
  730. * @param[in] idx An enum value indicating the index of pps instance
  731. * @retval hpm_stat_t @ref status_invalid_argument or @ref status_success
  732. */
  733. hpm_stat_t enet_set_ppsx_config(ENET_Type *ptr, enet_pps_cmd_config_t *cmd_cfg, enet_pps_idx_t idx);
  734. #if defined __cplusplus
  735. }
  736. #endif /* __cplusplus */
  737. /** @} */
  738. #endif /* HPM_ENET_DRV_H */